Solved

c# reflection - GetFields not returning values

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Formula for calculating ROI on training 6 51
Finding Events logs for IIS website that restarts 2 14
SSIS Start 2 21
JQuery Detecting page post back 6 13
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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