开发者

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

开发者 https://www.devze.com 2023-02-07 03:46 出处:网络
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 litt开发者_如何学Cle meta, so here\'s an example.

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 litt开发者_如何学Cle 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.


One way would be to get a MethodInfo representing the delegate-type's Invoke method, and then retrieve the method's return type.

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

精彩评论

暂无评论...
验证码 换一张
取 消