如何使用.NET反射来确定方法的返回类型(包括无效)和参数? [英] How to use .NET reflection to determine method return type (including void) and parameters?

查看:283
本文介绍了如何使用.NET反射来确定方法的返回类型(包括无效)和参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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确定返回类型,和<一href="http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getparameters.aspx"><$c$c>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))

这篇关于如何使用.NET反射来确定方法的返回类型(包括无效)和参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆