Silverlight 应用程序中拦截属性的问题

发布于 2024-09-05 19:51:08 字数 1822 浏览 3 评论 0原文

我在 Silverlight 应用程序中使用 Ninject 作为 DI 容器。现在我正在扩展应用程序以支持拦截,并开始集成 Ninject 的 DynamicProxy2 扩展。我试图拦截对 ViewModel 上的属性的调用,并最终得到以下异常:

“尝试访问方法失败:System.Reflection.Emit.DynamicMethod..ctor(System.String, System.Type, System.Type[] , System.Reflection.Module, Boolean)”

调用 invoking.Proceed() 方法时抛出此异常。我尝试了拦截器的两种实现,但它们都失败了

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));
    }
}

我想在设置属性值时调用 ViewModel 上的 OnPropertyChanged 方法。

我正在使用基于属性的拦截。

[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;
    }
}

我使用控制台应用程序测试了该实现,它工作正常。

我还在控制台应用程序中指出,只要我将 Ninject.Extensions.Interception.DynamicProxy2.dll 与 Ninject.dll 放在同一文件夹中,我就不必将 DynamicProxy2Module 显式加载到内核中,因为我必须为 Silverlight 应用程序显式加载它如下:

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

有人可以帮忙吗?谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

困倦 2024-09-12 19:51:08

由于安全问题,反射在 silverlight 中可能非常棘手。

检查Gabe对这个问题的回答,这是同样的问题。

好消息是,您可以使用动态而不是代理来实现您想要的相同功能。只需从 DynamicObject 扩展 ViewModel 并重写 TrySetMember 方法即可。

我希望它有帮助:)

Reflection can be really tricky in silverlight because of security issues.

Check Gabe's answer for this question, it's the same problem.

The good news is that you can achieve the same functionality you want using dynamic instead of proxies. Just extend your ViewModel from DynamicObject and override the TrySetMember method.

I hope it helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文