Issue intercepting property in Silverlight application

Posted by joblot on Stack Overflow See other posts from Stack Overflow or by joblot
Published on 2010-06-17T01:06:03Z Indexed on 2010/06/17 1:12 UTC
Read the original article Hit count: 433

I am using Ninject as DI container in a Silverlight application. Now I am extending the application to support interception and started integrating DynamicProxy2 extension for Ninject. I am trying to intercept call to properties on a ViewModel and ending up getting following exception:

“Attempt to access the method failed: System.Reflection.Emit.DynamicMethod..ctor(System.String, System.Type, System.Type[], System.Reflection.Module, Boolean)”

This exception is thrown when invocation.Proceed() method is called. I tried two implementations of the interceptor and they both fail

public class NotifyPropertyChangedInterceptor: SimpleInterceptor
{
    protected override void AfterInvoke(IInvocation invocation)
    {
        var model = (IAutoNotifyPropertyChanged)invocation.Request.Proxy;
        model.OnPropertyChanged(invocation.Request.Method.Name.Substring("set_".Length));
    }
}

public class NotifyPropertyChangedInterceptor: IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        invocation.Proceed();
        var model = (IAutoNotifyPropertyChanged)invocation.Request.Proxy;
        model.OnPropertyChanged(invocation.Request.Method.Name.Substring("set_".Length));
    }
}

I want to call OnPropertyChanged method on the ViewModel when property value is set.

I am using Attribute based interception.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class NotifyPropertyChangedAttribute : InterceptAttribute
{  
    public override IInterceptor CreateInterceptor(IProxyRequest request)
    {
        if(request.Method.Name.StartsWith("set_"))
            return request.Context.Kernel.Get<NotifyPropertyChangedInterceptor>();

        return null;
    }
}

I tested the implementation with a Console Application and it works alright.

I also noted in Console Application as long as I had Ninject.Extensions.Interception.DynamicProxy2.dll in same folder as Ninject.dll I did not have to explicitly load DynamicProxy2Module into the Kernel, where as I had to explicitly load it for Silverlight application as follows:

IKernel kernel = new StandardKernel(new DIModules(), new DynamicProxy2Module());

Could someone please help? Thanks

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about silverlight-3.0