8

At the moment I'm doing this (//edit: which is totally stupid as there's array which consequentily numbers its items in the same way that I did my Dictionary yesterday):

    Dictionary<int, Type> genericMap = new Dictionary<int, Type>
        {
            { 0, typeof(Action) },
            { 1, typeof(Action<>) },
            { 2, typeof(Action<,>) },
            { 3, typeof(Action<,,>) },
            { 4, typeof(Action<,,,>) },
            { 5, typeof(Action<,,,,>) },
            { 6, typeof(Action<,,,,,>) },
            { 7, typeof(Action<,,,,,,>) },
            { 8, typeof(Action<,,,,,,,>) },
        };

And somewhere else...

var parms = meth.GetParameters();
dType = genericMap[parms.Length].MakeGenericType(parms.Select(p => p.ParameterType).ToArray());

where meth is a MethodInfo.

Is there a more elegant way to do that? Or do I have to define a map like this to get the correct Action<> type dynamically that corresponds to the parameter count?

1
  • It would be helpful to know the purpose. If given a better idea of the larger problem you are trying to solve would be more likely to produce a quality answer for you.
    – konkked
    Apr 6, 2013 at 13:28

2 Answers 2

12

EDIT:

BTW, Do you know of Expression.GetActionType?

Creates a Type object that represents a generic System.Action delegate type that has specific type arguments.

Using that method, you could do:

dType = Expression.GetActionType( parms.Select(p => p.ParameterType).ToArray() );

Other ways:

var parms = meth.GetParameters();

int numArgs = parms.Length;

if(numArgs == 0)
{
    dType = typeof(Action);
}    
else 
{
   var rawType = Type.GetType("System.Action`" + numArgs);
   dType = rawType.MakeGenericType(parms.Select(p => p.ParameterType).ToArray());
}

But there's really nothing wrong with your approach really; being as explicit as possibile is often a better choice when it comes to reflection.

Also, here's another fancy way of building your map:

typeof(Action).Assembly
              .GetExportedTypes()
              .Where(type => type.FullName.StartsWith("System.Action") 
                                  && type.IsSubclassOf(typeof(Delegate)))
              .ToDictionary(type => type.GetGenericArguments().Length)
0
1

You can use array instead of dictionary, it's actually numbered from 0 to 8, just use array of types and access them with the index.

1
  • Aside from the fact that Ani's answer solves my problem perfectly, this is a classic case of self-facepalm: obviously I was so much in a hurry, yesterday, that I missed the forest for the trees... Thanks for this illuminating insight! Apr 7, 2013 at 8:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.