简体   繁体   中英

How do I get the return type of a delegate type through reflection?

I'm doing reflection-heavy work for a personal project, and I'd need to access the return type of a delegate through its Type object. This is a little meta, so here's an example.

Type type = typeof(Func<Foo, Bar, Baz>);
// ????
// Use reflection to come to the following expected result
Type result = typeof(Baz);

How can I do that?

I won't have any instance of that type to cast into Delegate .

一种方法是获取表示委托类型的Invoke方法的MethodInfo ,然后检索方法的返回类型。

var result = type.GetMethod("Invoke").ReturnType;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM