views:

82

answers:

1

how to know the number and type of parameters?

how to know the return type?

how to check whether the return type is void?

+7  A: 

Use MethodInfo.ReturnType to determine the return type, and MethodBase.GetParameters() to find out about the parameters. (MethodInfo derives from MethodBase, so once you've got the MethodInfo via Type.GetMethod etc, you can use both ReturnType and GetParameters().)

If the method is void, the return type will be typeof(void):

if (method.ReturnType == typeof(void))
Jon Skeet
how to check the voidness? and how to get MethodBase given a MethodInfo? thanks!
Louis Rhys
@Louis: MethodInfo derives from MethodBase, and you use `typeof(void)` to check for voidness.
Jon Skeet
will this do?`if(method.ReturnType.Equals(typeof(void)))`
Louis Rhys
@Louis: You could do that, but I would just use ==. I'll edit to demonstrate:
Jon Skeet
ok thanks a lot!
Louis Rhys
I didn't realise System.Void was an actual type - does this mean methods with void return types have actual values returned in IL?
Chris S
@Chris: Nope - it's sort of a placeholder type, really. It's useful to have a type representing the void precisely for things like ReturnType.
Jon Skeet