c# - Reflection says that interface method are virtual in the implemented type, when they aren't? -
i have following code in 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 + " 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 call by
assert.istrue(testmethodsof<myview, imyview>());
i want ensure methods interface declared virtual. reason because i'm applying spring.net aspect , apply virtual methods.
the problem i'm having implmember.isvirtual true, when not declared in declaring type.
what wrong testmethodsof logic?
cheers
all methods declared in interface marked virtual abstract
, , methods implement interface methods in classes marked virtual final
, clr knows can't call them directly - has vtable lookups @ runtime call right implementation. interface implementations still virtual, can't override them they're final.
as example, following c# definition:
public interface iinterface { void method(); } public class class : iinterface { public void method() {} }
compiles following 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() {} }
Comments
Post a Comment