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

2019-01-31 11:22发布

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?

1条回答
叼着烟拽天下
2楼-- · 2019-01-31 11:57
    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);
    }
查看更多
登录 后发表回答