Right now I have this code:
//some Method detection here..
var methodParams = method.GetParameters();
Type actionType = null;
switch(methodParams.Length)
{
case 1:
actionType = typeof(Action<>);
break;
case 2:
actionType = typeof(Action<,>);
break;
case 3:
actionType = typeof(Action<,,>);
break;
case 4:
actionType = typeof(Action<,,,>);
break;
case 5:
actionType = typeof(Action<,,,,>);
break;
}
var actionGenericType = actionType.MakeGenericType(methodParams.Select(x => x.ParameterType).ToArray());
I don't like this switch statement but I haven't yet found a way to select the Action generic overload based on the number of parameters (or based on any runtime int
).
Is there a more elegant way / oneliner to do something like this?
I don't want to use a dispatch table.
As I can see in the debugger, actionType
can be obtained as:
var actionType = Type.GetType("System.Action`" + methodParams.Length);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.