I’m pretty sure I know the answer to this (that you can’t do it), but I wanted to see if I’d missed something.
If I have classes:
public class Foo
{
}
public class Bar : Foo
{
}
And a method:
public Foo DoSomething()
{
return new Bar();
}
Am I correct in stating that there’s no way of knowing what the “true’ return type of DoSomething is without executing the method?, Meaning, if I just do this:
MethodInfo mi = this.GetType().GetMethod("DoSomething", BindingFlags.Public | BindingFlags.Instance);
//at this point, mi.ReturnType will be of type "Foo".
There’s no way of actually knowing that DoSomething actually returns a Bar, without executing the method and investigating the object i get back, correct?
You’re correct. There is no way to know the actual return type without executing the code, since the actual return type could depend on factors that are only known at runtime and can vary from one execution to another (for example, according to parameters passed to the method or return values of other methods).