使用反射来获得的MethodInfo,我想测试,如果返回的类型是typeof运算System.Void。

测试如果System.Int32正常工作

 myMethodInfo.ReturnType == typeof(System.Int32)

 myMethodInfo.ReturnType == typeof(System.Void)

不编译?目前林测试如果名称的字符串表示为“System.Void”,这似乎非常错误的。

有帮助吗?

解决方案

You can't use System.Void directly, but can access it using typeof(void).

Several people point out (here and in the comments here for example) that the reason for this is that the ECMA Standard 335, Partition II, section 9.4 says:

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)

其他提示

When I build this, I get the error:

System.Void cannot be used from C# -- use typeof(void) to get the void type object

Sounds like that's the answer...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top