Solved

c# reflection - GetFields not returning values

Posted on 2011-09-12
3
559 Views
Last Modified: 2013-12-17
Hi,

the following piece of code is returning no fieldInfo values:

Type type = ev.GetType();
                FieldInfo[] fields = type.GetFields();

Open in new window


where type is an instance of the following object:

 public class Event : iEvent
    {
        #region iEvent Members

        private string identifierName;

        public string IdentifierName
        {
            get { return identifierName; }
            set { identifierName = value; }
        }
        private int subclassId;

        public int SubclassId
        {
            get { return subclassId; }
            set { subclassId = value; }
        } 

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private DateTime startTime;

        public DateTime StartTime
        {
            get { return startTime; }
            set { startTime = value; }
        }
}

Open in new window


can somebody tell me how to make the fields readable?

Thanks
0
Comment
Question by:basil365
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36523103
Try this:
FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

Open in new window

0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 36523226
your field are not public, so add following BindingFlags to get those values:

FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
Type type = new Event().GetType();
            MemberInfo[] members = type.GetMembers();
            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);

            List<PropertyInfo> lstPropertyInfo = new List<PropertyInfo>();

            foreach (var item in members)
            {
                switch (item.MemberType)
                {
                    case MemberTypes.All:
                        break;
                    case MemberTypes.Constructor:
                        break;
                    case MemberTypes.Custom:
                        break;
                    case MemberTypes.Event:
                        break;
                    case MemberTypes.Field:
                        break;
                    case MemberTypes.Method:
                        break;
                    case MemberTypes.NestedType:
                        break;
                    case MemberTypes.Property:
                        lstPropertyInfo.Add(item as PropertyInfo);
                        break;
                    case MemberTypes.TypeInfo:
                        break;
                    default:
                        break;
                }
            }

Open in new window

0
 

Author Comment

by:basil365
ID: 36523501
apologies - i had already closed it before the 2nd reply
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now