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 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;
精彩评论