Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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.

like image 271
zneak Avatar asked Jan 29 '11 04:01

zneak


1 Answers

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;
like image 89
Ani Avatar answered Sep 27 '22 17:09

Ani