如何使用.NET反射来确定方法的返回类型(包括无效)和参数?如何使用、射来、参数、类型

2023-09-04 02:40:42 作者:耳根太软

如何知道的参数的数量和类型?

how to know the number and type of parameters?

怎么知道的返回类型?

如何检查返回值是否无效?

how to check whether the return type is void?

推荐答案

使用MethodInfo.ReturnType确定返回类型,和MethodBase.GetParameters()要了解的参数。 (的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))