Say I have a property like this in an abstract class AbstractClass:
protected MyClass MyProperty { get; set; }
And this property is used in a number of (virtual) methods in AbstractClass. In a subclass of the AbstractClass I want MyProperty to be a subclass of MyClass, is this possible or do I have to cast it?
MyProperty = new SubclassOfMyClass();
((SubclassOfMyClass)MyProperty).Method();
Doesn't look very nice... I have tried to use the 'new' keyword to hide MyProperty like this:
protected new SubclassOfMyClass MyProperty { get; set; }
But this did not work out as I thought it would, as it seems to creates a second MyProperty, and results in the one in AbstractClass always being null.
So I came up with something that may seem like a bit of a hack:
protected new SubclassOfMyClass MyProperty
{
get { return base.MyProperty as SubclassOfMyClass; }
set { base.MyProperty = value; }
}
Is there a better way to do this?