I need to pass a List of Types to a method, but I want to be sure (at compile time that is) that all of them inherit from BaseType. Also, I dont know how many Types have to be passed.
So I figured this would be a bad way to go:
public void DoSomething(params Type[] types)
So what I ended up doing up to now is something like this:
private void DoSomething(params Type[] types)
public void DoSomething<T1>()
where T1 : BaseType
{
DoSomething(typeof(T1));
}
public void DoSomething<T1, T2>()
where T1 : BaseType
where T2 : BaseType
{
DoSomething(typeof(T1), typeof(T2));
}
public void DoSomething<T1, T2, T3>()
where T1 : BaseType
where T2 : BaseType
where T3 : BaseType{...}
// and so on...
You get the Idea. So the Question is: can you do this a bit more beautifully? Because this does not support an arbitrary number of types. And in my scenario, eight or more types would not be too uncommon.
I want to use this for "magic" of this kind but the caller does not have a reference to the container.