I would like to make a delegate that calls a special instance method, unfortunately it seems that if this method is virtual, instead of the base version method, a method override for the inherited class will be called.
public class Base{ public virtual void Method(){ Console.WriteLine("Base"); } } public class Child : Base{ public override void Method(){ Console.WriteLine("Child"); } }
If somewhere else in the code there is the following:
var action = Delegate.CreateDelegate(typeof(Action<Base>), typeof(Base).GetMethod("Method")) as Action<Base>; action(new Child());
The output of this program is Child . I would really like it to be Base . I tried the same with expression trees, and I get the same result as the emitted IL uses the callvirt method. The only way to do something like this with Reflection.Emit ?
I request that I use a type builder to override class behavior. If I wrote a method myself, I could just go to base.Method() or something else, but some of the behavior methods can be determined dynamically only at runtime, since taking into account many possible cases will be very tedious.
Since I am creating a class derived from Base at runtime, if I try to call Method() inside the Method() overload that I am doing, this leads to endless recursion and exceptions. (not very good).
This is for an AOP style project where I add some logic to the methods at runtime. I tag methods with attributes, then I have a type builder that creates methodBuilders that feed the body of the method constructor using the expression tree using CompileToMethod(methodbuilder) http://msdn.microsoft.com/en-us/library/dd728224 .aspx , This is a ton lighter than reflection.emit, because the logic is nontrivial, which I add. The goal is that I have a factory, spits out a new class, which, when I call Method() , first executes some logic before ultimately invoking the base implementation.