简体   繁体   English

反思说接口方法在实现类型中是虚拟的,不是吗?

[英]Reflection says that interface method are virtual in the implemented type, when they aren't?

I have the following code in an unit test 我在单元测试中有以下代码

    public bool TestMethodsOf<T, I>()
  {
   var impl = typeof(T);
   var valid = true;

   foreach (var iface in impl.GetInterfaces().Where(i => typeof(I).IsAssignableFrom(i)))
   {

    var members = iface.GetMethods();

    foreach (var member in members)
    {
     Trace.Write("Checking if method " + iface.Name + "." + member.Name + " is virtual...");
     var implMember = impl.GetMethod(member.Name, member.GetParameters().Select(c => c.ParameterType).ToArray());
     if (!implMember.IsVirtual)
     {
      Trace.WriteLine(string.Format("FAILED"));
      valid = false;
      continue;
     }

     Trace.WriteLine(string.Format("OK"));
    }
   }
   return valid;
  }

which I call by 我称之为

Assert.IsTrue(TestMethodsOf<MyView, IMyView>());

I want to ensure that all the methods from the interface are declared as virtual. 我想确保接口中的所有方法都声明为虚拟方法。 The reason is because I'm applying a spring.net aspect and it will only apply to virtual methods. 原因是因为我正在应用spring.net方面,它将仅适用于虚拟方法。

The problem I'm having is that implMember.IsVirtual is always true, even when they are not declared as so in the declaring type. 我遇到的问题是,即使在声明类型中未声明为implMember.IsVirtual始终为true。

What is wrong with my TestMethodsOf logic? 我的TestMethodsOf逻辑有什么问题?

Cheers 干杯

All methods declared in an interface are marked as virtual abstract , and all methods that implement interface methods in classes are marked as virtual final , so the CLR knows it can't just call them directly - it has to do vtable lookups at runtime to call the right implementation. 接口中声明的所有方法都标记为virtual abstract ,而所有在类中实现接口方法的方法都标记为virtual final ,因此CLR知道它不能直接调用它们-它必须在运行时进行vtable查找才能调用正确的实施。 The interface implementations are still virtual, but you can't override them as they're final. 接口实现仍然是虚拟的,但是您不能覆盖它们,因为它们是最终的。

As an example, the following C# definition: 例如,以下C#定义:

public interface IInterface {
    void Method();
}

public class Class : IInterface {
    public void Method() {}
}

compiles to the following IL: 编译为以下IL:

.class public interface abstract IInterface {
    .method public abstract virtual instance void Method() {}
}

.class public Class extends [mscorlib]System.Object implements IInterface {
    .method public specialname rtspecialname instance void .ctor() {}
    .method public virtual final instance void Method() {}
}

我相信当您实现接口时,从该接口继承的方法会自动标记为虚拟,因此逻辑很好,并且不需要测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用反射确定已实现接口的通用类型? - How do I determine the generic type of an implemented interface using reflection? 实现的方法的返回类型接口,Ienumerator - Return type interface for the method implemented, Ienumerator 返回派生类型时,“未实现接口” - “Interface not implemented” when Returning Derived Type 从接口拦截时获取实现的方法 - Get implemented method when intercepting from an interface C#-实现类型接口时,不允许我将“ this”作为类型返回 - C# - Doesn't allow me to return “this” as a type when interface for type is implemented 如何通过只有一个类型而不使用反射来访问一个类型实现的 static 抽象接口成员? - How to access a type's implemented static abstract interface members by having only a type without using reflection? 如何使用C#和Reflection获取实现类类型而不是接口类型 - How to use C# and Reflection to get Implemented Class type not the Interface type C# - 通过子类实现的接口继承和使用时,找不到方法的用法 - C# - Can't find usage of method when inherited and used through an interface implemented by the subclass 如何使用Type.InvokeMember来调用显式实现的接口方法? - How to use Type.InvokeMember to call an explicitly implemented interface method? 使接口方法返回实现它的类的类型的对象 - Make a interface method return an object of type of the class which implemented it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM