如何使用.NET反射来确定方法的返回类型(包括无效)和参数?
问题描述:
如何知道的参数的数量和类型?
how to know the number and type of parameters?
怎么知道的返回类型?
如何检查返回值是否无效?
how to check whether the return type is void?
答
使用MethodInfo.ReturnType$c$c>确定返回类型,和MethodBase.GetParameters()$c$c>要了解的参数。 (的MethodInfo
从 MethodBase
派生,所以一旦你已经得到了的MethodInfo
通过 Type.GetMethod
等,可以同时使用的返回类型
和 GetParameters()
)
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()
.)
如果该方法无效
,返回类型为 typeof运算(无效)
:
If the method is void
, the return type will be typeof(void)
:
if (method.ReturnType == typeof(void))