lundi 2 mars 2015

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();
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 B or C class.


Can someone please suggest me how to achieve this?


Thanks in advance.






Aucun commentaire:

Enregistrer un commentaire