Can I get the signature of a C# delegate by its type?

C#ReflectionDelegates

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

    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);
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfastcallView Question on Stackoverflow
Solution 1 - C#Marc GravellView Answer on Stackoverflow