Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reflecting and Inheritence

Status
Not open for further replies.

PGO01

Programmer
Jan 8, 2008
156
GB
When I do myObj.GetType().GetFields() I don't see fields from base classes. Is there a way to do this with BindingFlags or another way?

I guess I could do some kind of recursive myObj.GetType().BaseType and get the fields that way but thought there might be an easier way.

Regards
Pete
 
Yes they are non-static and public but they belong to the base class.



 
and you are sure they are fields? I ask because fields are typically used for private values, not public. they are encapsulated by either properties or methods. examples:
Code:
class MyClass
{
    private object field;
 
    public object Property {get{return field;}}

    public void ChangeField(object newValue)
    {
        field = newValue;
    }
}
not that public fields are not possible. it' just that properties and functions lend themselves to OOP techniques where as fields do not.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
They are private fields (sorry for the mistake) but they relate to the public properties. i.e.

Code:
    public class A
    {
        private string _AProp;
        public string AProp
        {
            get { return _AProp; }
            set { _AProp = value; }
        }
    }

Now if I create a class B that inherits from A, and reflect through an instance of the B object, I don't see _AProp in the GetFields() (with BindingFlags.Instance | BindingFlags.NonPublic)

Is this correct?
 
why reflect the static field when you have access to public properties?

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I'm presuming it's the same with properties, i.e. GetProperties() will only get properties in the current layer of inheritance and not the base classes?

I'll submit my actual code to give you a better idea of what I am trying to achieve.

Regards,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top