c# - 如何测试 MethodInfo.ReturnType 是否为 System.Void 类型?

标签 c# reflection methodinfo

使用反射获取一个MethodInfo,我想测试返回的类型是否为typeof System.Void。

测试它是否是 System.Int32 工作正常

 myMethodInfo.ReturnType == typeof(System.Int32)

但是

 myMethodInfo.ReturnType == typeof(System.Void)

不编译?目前我正在测试名称的字符串表示是否为“System.Void”,这似乎是非常错误的。

最佳答案

您不能使用 System.Void 直接,但可以使用 typeof(void) 访问它.

一些人指出(例如 here 和评论中的 here)这是因为 ECMA Standard 335 ,第二部分,第 9.4 节说:

The following kinds of type cannot be used as arguments in instantiations (of generic types or methods):

  • Byref types (e.g., System.Generic.Collection.List 1<string&> is invalid)
  • Value types that contain fields that can point into the CIL evaluation stack (e.g.,List<System.RuntimeArgumentHandle>)
  • void (e.g., List<System.Void> is invalid)

关于c# - 如何测试 MethodInfo.ReturnType 是否为 System.Void 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1820243/

相关文章:

java - 使用Java反射获取类数组的类型

c# - 如何获取接口(interface)方法的MethodInfo,实现了类方法的MethodInfo?

c# - 使用 API 更新 eBay 上的订单状态?

c# - Fieldinfo.FieldType.FullName 未提供日期时间和 double 的数据类型

c# - 将现有的 c# 移植到 java(很多 pinvoke/user32.dll)——陷阱?

java - 使用反射创建新对象?

c# - 项目的加载程序集

c# - xUnit.net 理论,其中 MemberData 来自派生类

c# - ListViewItem 覆盖默认样式打破 gridviews