使用反射获取一个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/