I am looking for a solution for accessing the flatten (lowest) values of a class property and its derivative by reflection with property names.
those. access Property1 or Property2 from class ClassB or ClassC:
public class ClassA
{
public virtual object Property1 { get; set; }
public object Property2 { get; set; }
}
public class ClassB : ClassA
{
public override object Property1 { get; set; }
}
public class ClassC : ClassB
{
}
Using simple reflection works until you have restored virtual properties (i.e. Property1 from ClassB). Then you get an AmbiguousMatchException because the crawler does not know if you want a property of the main class or derivative.
Using BindingFlags.Declared; Avoid AmbiguousMatchException exceptionally, but the properties of unoverrided virtual properties or derived classes are omitted (i.e. Property2 from ClassB).
:
PropertyInfo propertyInfo = _type.GetProperty(propertyName, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
if (propertyInfo == null)
propertyInfo = _type.GetProperty(propertyName);
, 2- : Property1 ClassC AmbiguousMatchException.
: , ... Erk...??
Emit, Lambda ( Expression.Call ?) DLR-.
!