I have a base class Ref<>:
public class Ref<T>
{
public T Value;
public Ref() { }
public Ref(T initialValue)
{
Value = initialValue;
}
}
and a derived class RefProperty<>:
public class RefProperty<T> : Ref<T>
{
public Func<T> Getter;
public Action<T> Setter;
public T Value
{
get { return Getter(); }
set { Setter(value); }
}
public RefProperty(Func<T> getter, Action<T> setter)
{
Getter = getter;
Setter = setter;
}
}
I then declare a Ref and initialize it as a RefProperty (polymorphism):
Ref<int> IntDoubled = new RefProperty<int>(getIntDoubled, setIntDoubled);
Where getIntDoubled and setIntDoubled are methods as expected:
private int getIntDoubled()
{ return myInt * 2; }
private void setIntDoubled(int value)
{ myInt = value / 2; }
and where myInt is a declared test integer:
int myInt = 10;
I then print:
Console.WriteLine(IntDoubled.Value);
I hope it would return 20 since the property called Value in the derived class, IntDoubled, calls the getIntDoubled() method which returns myInt*2. But since IntDoubled is declared as a Ref and not a RefProperty, it returns the Value field of the base class instead (which returns 0 as the value is not set).
So the question is: How can I get a derived class' property instead of a base class' field of the same name if the instance is polymorphed?