简体   繁体   中英

How to get value of property from object of class at runtime using reflection

I have a class something like below:

Class A : B<C>
{
    public A(C entity):base(entity)
    {}
}

abstract class B<T>
{
    public B(T entity)
        {
            Entity = entity;
        }

        public T Entity { get; private set; }
}

Class C: D
{
    public string prop2{get;set;}
}
Class D
{
    public string prop1{get;set;}
}
 Main()
 {
 A obj = new A(new C());
 obj.GetType().GetProperty("prop1",  BindingsFlag.Instance|BindingsFlag.FlatteredHierarchy)//  is null


 }

I have object of class A. I want to get property value from this object at runtime.

I am trying with

obj.GetType().GetProprty("propertyName", 
                         BindingsFlag.FlattenHierarchy).GetValue(obj, null);

However GetProprty() is returing null as that property is declared either in D or C class.

Can someone please suggest me how to achieve this?

Thanks in advance.

GetType().GetProperty("propertyName", BindingsFlag.FlattenHierarchy)
         .GetValue(obj, null);

You are misssing binding flag that specifies wheter get instance or static property:

 BindingsFlag.FlattenHierarchy | BindingsFlag.Instance

According to MSDN flag BindingsFlag.Instance or BindingsFlag.Static must be specifed explicity in order to get not null values:

You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

What's more, public properties are exlcuded by default. So if you property is public you need to specify additional flag:

BindingsFlag.FlattenHierarchy | BindingsFlag.Instance | BindingsFlag.Public

Remarks:

Specify BindingFlags.Public to include public properties in the search.

If property in base is private, FlattenHierarchy will not enumerate it:

(...) private static members in inherited classes are not included If this is your case, I am afraid that you have to manually travel through base class and search for that property.

Make also sure, that property name is valid and exists.

EDIT: After your edit, I see the problem. Your class A is not sub-class of D class (you want to get property from D class). That is why getting property value is not working in such way. You need to follow the following steps:

// get entity prop value
var entityValue =
    (obj.GetType()
        .GetProperty("Entity", 
           BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public)
        .GetValue(obj));
// get prop value
var prop1Value =
    entityValue.GetType()
               .GetProperty("prop1", 
                  BindingFlags.FlattenHierarchy | 
                  BindingFlags.Instance | 
                  BindingFlags.Public)
               .GetValue(entityValue);

Remember to handle null values etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM