I have a collection of classes descending from a single root class
(let's call it RootClass). They all currently have a property of
Logical, of type Logical. However they actually return a subclass of
Logical (LogicalA, LogicalB). I'm currently casting them to the right
type when I retrieve them, but obviously this isn't terribly 'nice'.
I tried converting the class to be generic, and having a type parameter
that affected the type of the Logical property. But then I couldn't
have a single pointer variable that could point to any instance of the
class, as you can't cast from one generic class to another - i.e. you
can't have
RootClass<object= RootClass<TextBox>;
and some of my code depends on keeping a point to the currently active
class.
So, I thought, I'll try creating a separate method
GetLogical<LogicalTypeand then converting it from an open to a closed
type in the sublclass.
But it seems you can't close an open generic method as:
class BaseReturner
{
public virtual T GetLogical<T>() where T : new()
{
return default(T);
}
class TextBoxReturner : BaseReturner
{
public override TextBox GetLogical<Windows.Forms.TextBox>()
{
return new TextBox();
}
}
returns an error.
Is there any way of doing this, or should I just go back to casting
things back and forth?
Cheers,
Andy