Imagine I have an interface that specifies some collection handler:
interface ICollectionHandler<T> where T : new() { ... }
I then have same other class that wants a collection handler, which it will use for various types, so I want to do something like:
class SomeClass<T> where T : ICollectionHandler<> ...
If I do this though, I get an error saying that the “Type argument is missing”.
So is there a way of specifying that T is a generic type, whose own type argument can be specified at runtime, or am I pushing C# beyond its (possibly sensible) boundaries here?
You can create a base interface for
ICollectionHandler<T>and constrain against it.Or add a parameter to
SomeClassrepresenting the Type that should be passed into theICollectionHandler<T>constraint: