Solved

c# reflection - GetFields not returning values

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

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

11 Experts available now in Live!

Get 1:1 Help Now