如何使用.NET反射来确定方法的返回类型(包括无效)和参数? [英] How to use .NET reflection to determine method return type (including void) and parameters?
问题描述
如何知道的参数的数量和类型?
how to know the number and type of parameters?
怎么知道的返回类型?
如何检查返回值是否无效?
how to check whether the return type is void?
推荐答案
使用<一个href="http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.returntype.aspx"><$c$c>MethodInfo.ReturnType$c$c>确定返回类型,和<一href="http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getparameters.aspx"><$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))
这篇关于如何使用.NET反射来确定方法的返回类型(包括无效)和参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!