I'm working with some generics in C#. I have an abstract generic class:
public abstract class BaseClass<T> where T : Foo { }
and the type parameter is specified when other classes inherit from the base class.
I'm also trying to write some code for another abstract class that needs to store one of these base classes, but it won't know the parameter of the base class (and it doesn't need to know). In Java I could simple write:
protected BaseClass<?> myBaseClass;
But in C# it insists I give it a type parameter. If I declare it as:
protected BaseClass<Foo> myBaseClass;
I cannot assign any parametrized values of BaseClass
to it.
Is there a work around to achieve the effect of BaseClass<?>
in C#? Obviously there are ways to restructure my code to avoid the need, such as parameterizing all of the classes that use BaseClass
as well, but this would be less than ideal. Any help would be appreciated!
To match Java's behavior somewhat in C#, it is common to put an additional non-generic interface in the inheritance hierarchy.
public interface IBaseClass
{
Foo GetValue();
void SetValue(Foo value);
}
public abstract class BaseClass<T> : IBaseClass
where T : Foo
{
public T GetValue<T>()
{ /* ... */ }
public void SetValue<T>(T value)
{ /* ... */ }
Foo IBaseClass.GetValue() // Explicit interface method implementation
{
return (Foo)GetValue<T>();
}
void IBaseClass.SetValue(Foo value) // Explicit interface method impl.
{
SetValue<T>((T)value);
}
}
And then use IBaseClass
where you need BaseClass<?>
:
IBaseClass myClass;
Foo f = myClass.GetValue();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With