62

Is there a straightforward way using reflection to get at the parameter list for a delegate if you have its type information?

For an example, if I declare a delegate type as follows

delegate double FooDelegate (string param, bool condition);

and later get the type information for that delegate type as follows

Type delegateType = typeof(FooDelegate);

Is it possible to retrieve the return type (double) and parameter list ({string, bool}) from that type info object?

fastcall
  • 2,564
  • 4
  • 20
  • 21

1 Answers1

111
    MethodInfo method = delegateType.GetMethod("Invoke");
    Console.WriteLine(method.ReturnType.Name + " (ret)");
    foreach (ParameterInfo param in method.GetParameters()) { 
        Console.WriteLine("{0} {1}", param.ParameterType.Name, param.Name);
    }
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 12
    Perfect! Digging deeper, the reason this works is that declaring the delegate is basically syntax sugar for declaring a class derived from Delegate with a new Invoke method that takes the specified parameters. Thanks for the help. – fastcall Jan 09 '09 at 20:49