views:

710

answers:

1

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?

+16  A: 
    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
+10 points to throw in your rep pool. Nothing but a tiny ripple in a huge lake by now, right?
Jeff Yates
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
@ffpf, nah - I maxed out my daily cap ages ago... but the vote helped me get another bronze, so ta ;-p
Marc Gravell