I have a piece of code where sometimes I need to create a new generic type, but with an unknown number of generic parameters. For instance:
public object MakeGenericAction(Type[] types) { return typeof(Action<>).MakeGenericType(paramTypes); }
The problem is that if there is more than one type in my array, the program will crash. In the short term, I came up with something like this as an intermediate gap.
public object MakeGenericAction(Type[] types) { if (types.Length == 1) { return typeof(Action<>).MakeGenericType(paramTypes); } else if (types.Length ==2) { return typeof(Action<,>).MakeGenericType(paramTypes); } ..... And so on.... }
It really works and is easy enough to cover my scripts, but it seems really hacked. Is there a better way to handle this?
source share