Reputation: 4584
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?
Upvotes: 1
Views: 103
Reputation: 477
Correct.
I would assume the point of the DoSomething method would be to return a specific derived type, and be able to use it for general purposes as a Foo.
In that case I'm not sure what the advantage would be to knowing the derived type to be returned, but that's probably not your question. :)
Upvotes: 0
Reputation: 25201
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).
Upvotes: 1