如何在 C# 中使用 System.Reflection.MethodBase 查找方法的返回类型?
如何从 MethodBase 中找出方法的返回类型?我正在使用 PostSharp 并尝试重写 CompileTimeValidate(MethodBase method) 方法,以确保该属性应用于具有正确签名的方法。
谢谢,
how do I find out the return type of a method from the MethodBase? I'm using PostSharp and trying to override the CompileTimeValidate(MethodBase method) method to make sure the attribute is applied to a method with the correct signature.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MethodBase 用作 MethodInfo 的基类,它具有属性 ReturnType。
您可以尝试转换为 MethodInfo 的实例并检查该属性。
MethodBase is used as a base class of MethodInfo which has a property ReturnType.
You could try and cast to an instance of MethodInfo and check that property.
MethodBase 本身没有返回类型,因为除了普通方法之外,它还用于表示没有返回类型的方法,例如构造函数。相反,您需要查看它是否是
MethodInfo
的实例,并检查ReturnType
属性。MethodBase
itself does not have a return type because in addition to normal methods it also is used to represent methods, such as constructors, which have no return type. Instead you need to see if it's an instance ofMethodInfo
and check that for theReturnType
property.尝试这样的事情。
MethodInfo
具有该属性,但MethodBase
也用于构造函数,并且它们没有返回类型。Try something like this.
MethodInfo
has the property butMethodBase
is used for constructors as well, and they do not have a return type.尝试使用
MethodInfo.ReturnType
属性。要获取返回类型属性,首先获取
Type
。从Type
中获取MethodInfo
。从MethodInfo
中获取ReturnType
。看来你不能用 MethodBase 做到这一点...
http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.returntype.aspx
Try the
MethodInfo.ReturnType
property.To get the return type property, first get the
Type
. From theType
, get theMethodInfo
. From theMethodInfo
, get theReturnType
.It seems like you can't do it with MethodBase...
http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.returntype.aspx