如何确定所述的MethodInfo是基本方法的重写重写、所述、基本、方法

2023-09-03 01:23:22 作者:不必在乎太多

我想,以确定是否是我从一个类型实例的GetMethod的电话得到的MethodInfo对象由类型实现的,或它的基础。

例如:

 富富=新的Foo();
。MethodInfo的MethodInfo的= foo.GetType()GetMethod的(的ToString的BindingFlags |实例);
 

ToString方法可在Foo类或未实现。我想知道,如果我得到了富执行?

  Java方法的重载与重写

相关问题

     

Is可以说,如果一个.NET虚方法已经被重写在派生类中?

解决方案

检查其 DeclaringType 属性。

 如果(methodInfo.DeclaringType == typeof运算(富)){
   // ...
}
 

I'm trying to determine if the MethodInfo object that I get from a GetMethod call on a type instance is implemented by the type or by it's base.

For example:

Foo foo = new Foo();
MethodInfo methodInfo = foo.GetType().GetMethod("ToString",BindingFlags|Instance);

the ToString method may be implemented in the Foo class or not. I want to know if I'm getting the foo implementation?

Related question

Is it possible to tell if a .NET virtual method has been overriden in a derived class?

解决方案

Check its DeclaringType property.

if (methodInfo.DeclaringType == typeof(Foo)) {
   // ...
}