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
.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With