Solved

c# reflection - GetFields not returning values

Posted on 2011-09-12
3
561 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

803 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