ScottSEA ScottSEA - 4 months ago 15
C# Question

C# Generic Types :: How can I specify BaseType<any>?

I have a

WrapperBase<PocoBase>
class that wraps POCO objects and provides additional functionality:

public class WrapperBase<T>
where T : PocoBase


and several implementations on top of that base class.

public class ButtonWrapper : WrapperBase<Button>
public class ZipperWrapper : WrapperBase<Zipper>


I'm trying to write a class that takes a
WrapperBase<T>
as a constructor parameter.

public class ThingDoer
where T : PocoBase
{
public ThingDoer(WrapperBase<T> wrapper)
{
}
}


but no matter what I try, the
ThingDoer
wants a definition for
T
:

new ThingDoer<Zipper>(zipperWrapper);
// I don't care what T is... get rid of <Zipper>


How can I say "I don't care what
T
is, because all the functionality I want is in
WrapperBase
"?

Answer

How can I say "I don't care what T is, because all the functionality I want is in WrapperBase"?

You could define an interface IWrapperFunctionality that specifies the methods providing that functionality. Have WrapperBase<T> implement that interface, and use it as your constructor argument.

public ThingDoer(IWrapperFunctionality wrapper)
{ 
}