如何检查类型内的方法是否被重写?反射 C#
可能的重复:
确定 Equals() 是否是覆盖?
我需要运行特定的部分特定类型是否覆盖Object.Equals
的代码。
如何检查类型是否覆盖此方法?
在 VS 中输入 typeof(mytype).GetMethod("Equals").
会带来一系列选项,但没有“IsOverriden”或类似的东西。
Possible Duplicate:
Determine if Equals() is an override?
I need to run specific pieces of code for whether a specific type overrides Object.Equals
or not.
How can I check if a type overrides this method?
Typing typeof(mytype).GetMethod("Equals").
in VS brings me a wide list of options, but there's no "IsOverriden" or something like that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查
DeclaringType
是否为typeof(object)
。Check whether
DeclaringType
istypeof(object)
.有两种方法,首先,
MethodInfo
类有一个DeclaringType
,您可以使用它来查看它是否是System.Object
。其次,您可以使用接受BindingFlags
枚举并传入DeclaredOnly
的GetMethod
重载来确保您不会获取任何父对象。There are two ways, first the
MethodInfo
class has aDeclaringType
you can use to see if it isSystem.Object
. Second you can use the overloads ofGetMethod
that accept aBindingFlags
enum and pass inDeclaredOnly
to ensure you don't get any parent objects.您可以使用绑定标志来发挥自己的优势:
当且仅当
mytype
覆盖Equals
时,t
才不是null
。You can use the binding flags to your advantage:
Then
t
is notnull
if and only ifmytype
overridesEquals
.