I would like to be able to dynamically get the value of an overriden property at runtime using reflection. For example,
class A {
public virtual int Foo => 5;
//This implementation doesn't work
public int ParentFoo => (int)this.GetType().BaseType.GetProperty(nameof(Foo)).GetValue(this);
}
class B : A {
public override int Foo => 7;
}
var test = new B();
Console.WriteLine(test.Foo); //"7"
Console.WriteLine(test.ParentFoo); //Should display "5"
The point of doing this is that the type hierarchy is fairly deep and I don't want extenders to have to re-implement ParentFoo
each time with the exact same logic (public int ParentFoo => base.Foo;
). I don't mind paying the performance cost for reflection- this property doesn't need to be performant.
Is it possible to accomplish what I need here using reflection?
Aucun commentaire:
Enregistrer un commentaire