I'm having a hard time to understand why it's returning the base value and not the actual value. I need to be sure there is a Prop
, but I want to use the actual value and not the base value.
public interface IA
{
string Value { get; set; }
}
public class A : IA
{
public String Value { get; set; }
}
public class B
{
public IA Prop { get; set; }
}
public class C : B
{
public new A Prop { get; set; }
public C()
{
Prop = new A();
Prop.Value = "test C";
}
}
public static class D
{
public static string GetDynamicProp(dynamic item)
{
return item.Prop.Value;
}
public static string GetProp<T>(T item) where T : B
{
return item.Prop.Value;
}
}
And then using it:
var test = new C();
Console.WriteLine(test.Prop.Value); // test C
Console.WriteLine(C.GetDynamicProp(test)); // test C
Console.WriteLine(D.GetProp(test)); // null reference exception because it is using the `IA Prop` from `B`
When using a dynamic type it does work, but then I can't use intellisense for example. So I would like to make sure the object send to D.GetProp
implements a property of IA
, but still use the actual value.
I want the Prop
to always be of a type that implements IA
, but it can be an inherited type.