[英]How do I get the return type of a delegate type through reflection?
我正在为个人项目进行反思繁重的工作,我需要通过其Type
对象访问委托的返回类型。 这是一个小元,所以这是一个例子。
Type type = typeof(Func<Foo, Bar, Baz>);
// ????
// Use reflection to come to the following expected result
Type result = typeof(Baz);
我怎样才能做到这一点?
我不会将任何类型的实例强制转换为Delegate
。
一种方法是获取表示委托类型的Invoke
方法的MethodInfo
,然后检索方法的返回类型。
var result = type.GetMethod("Invoke").ReturnType;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.