Dynamic Proxy 2: Mixins

time to read 25 min | 4873 words

I needed a rest from dealing with SSIS and data migrations issues, so I decided to put some time in real code. I just finished adding support for Mixins to Dynamic Proxy 2. Here is the first test that passed, the name should tell you quite a bit about what is required to make it work.


public void CanCreateSimpleMixinWithoutGettingExecutionEngineExceptionsOrBadImageExceptions()


       ProxyGenerationOptions proxyGenerationOptions = new ProxyGenerationOptions();

       proxyGenerationOptions.AddMixinInstance(new SimpleMixin());

       object proxy = generator.CreateClassProxy(

              typeof(object), proxyGenerationOptions, new AssertInvocationInterceptor());


       Assert.IsTrue(proxy is ISimpleMixin);




The generated proxy looks something like this one:

public class ObjectProxyefb7dccd21fe43b5b2d13c788dce3bdb : ISimpleMixin


    public IInterceptor[] __interceptors;

    public ISimpleMixin __mixin_Castle_DynamicProxy_Test_Mixins_ISimpleMixin;

    public static MethodInfo tokenCache1 = ((MethodInfo) methodof(ISimpleMixin.DoSomething, ISimpleMixin));

    public static Type typeTokenCache = typeof(object);


    public ObjectProxyefb7dccd21fe43b5b2d13c788dce3bdb(ISimpleMixin mixin1, IInterceptor[] interceptorArray1)


        this.__mixin_Castle_DynamicProxy_Test_Mixins_ISimpleMixin = mixin1;

        this.__interceptors = interceptorArray1;



    public override int DoSomething()


        object[] objArray = new object[0];

        InvocationDoSomething_1 g_ = new InvocationDoSomething_1(
             this.__interceptors, typeTokenCache, tokenCache1, objArray, this);


        return (int) g_.ReturnValue;




    public sealed class InvocationDoSomething_1 : AbstractInvocation


        public ISimpleMixin target;


        public InvocationDoSomething_1(ISimpleMixin mixin1, IInterceptor[] interceptorArray1, Type type1, MethodInfo info1,
object[] objArray1, object obj1) : base(mixin1, obj1, interceptorArray1, type1, info1, objArray1)


            this.target = mixin1;



        public sealed override void InvokeMethodOnTarget()


            int num = this.target.DoSomething();

            base.ReturnValue = num;





The code is in the repository, if you feel like taking it out for a spin.