DynamicProxy2:CreateClassProxyWithTarget +拦截器

发布于 2024-11-07 09:18:34 字数 2307 浏览 4 评论 0原文

如果我在另一个问题中错过了这一点,我深表歉意;我想了很长时间才决定我有一个独特的问题...我想使用 DynamicProxy2 为 WPF 应用程序的模型类提供拦截。这样我就不必到处完全实现 INotifyPropertyChanged。例如,一旦被代理和拦截,下面的类应该完全参与双向数据绑定:

public class ModelExample : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public int Id{ get; set; }
    public string Uri{ get; set; }
    public string Name{ get; set; }
}

我发现我可以创建模型类的新实例并通过调用 CreateClassProxy 方法拦截对其的调用:

new ProxyGenerator().CreateClassProxy<T>(interceptors);

不幸的是,这迫使我允许 ProxyGenerator 类创建我的模型实例,并且我从中间层获取它们,即它们已经存在。我需要包装现有对象,因此我认为我需要调用CreateClassProxyWithTarget

new ProxyGenerator().CreateClassProxyWithTarget(instance, interceptors);

但是,当我这样做时,我的拦截器将停止运行。我很确定这不是拦截器的错......它是一个非常简单的对象。这是它的接口:

public interface IFluentInterceptor : IInterceptor
{
    IFluentInterceptor Before(Action<IInvocation> before);
    IFluentInterceptor After(Action<IInvocation> after);
    IFluentInterceptor Finally(Action<IInvocation> @finally);
    IFluentInterceptor RunCondition(Func<IInvocation, bool> runCondition);
    IFluentInterceptor OnError(Func<IInvocation, Exception, bool> onError);
}

FluentInterceptor 类型实现了它。 BeforeAfter等方法过于简单,无法展示;它们都添加到要在方法调用期间使用的操作队列,然后每个方法都返回 this,从而允许方法链接。

下面的代码不起作用,但我不明白为什么:

new ProxyGenerator().CreateClassProxyWithTarget(instance, new FluentInterceptor()
    .After(invocation =>
    {
        if (!invocation.Method.Name.StartsWith("set_")) return;
        string propertyName = invocation.Method.Name.Substring(4);
        FieldInfo info = invocation.TargetType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
            .Where(f => f.FieldType.Equals(typeof (PropertyChangedEventHandler)))
            .FirstOrDefault();
        if (info == null) return;
        var handler = info.GetValue(invocation.InvocationTarget) as PropertyChangedEventHandler;
        if (handler != null) handler.Invoke(invocation.TargetType, new PropertyChangedEventArgs(propertyName));
    }));

如果我用 CreateClassProxy 尝试,它就像一个魅力。有人看到我做错了什么吗?

谢谢!

If I've missed this in another question I apologize; I looked for a good while before deciding I had a unique question... I want to use DynamicProxy2 to provide interception for a WPF application's model classes. This is so that I do not have to fully implement INotifyPropertyChanged everywhere. For instance, the below class should fully participate in two-way data binding once proxied and intercepted:

public class ModelExample : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public int Id{ get; set; }
    public string Uri{ get; set; }
    public string Name{ get; set; }
}

I have found that I can create a new instance of the model class and intercept calls to it by calling the CreateClassProxy method:

new ProxyGenerator().CreateClassProxy<T>(interceptors);

Unfortunately, this forces me to allow the ProxyGenerator class to create my model instances, and I'm getting those back from my middle tier, i.e. they already exist. I need to wrap existing objects, so I think I need to call CreateClassProxyWithTarget instead:

new ProxyGenerator().CreateClassProxyWithTarget(instance, interceptors);

However, when I do that my interceptor stops functioning. I'm pretty sure it's not the interceptor's fault... it's a very simple object. Here's its interface:

public interface IFluentInterceptor : IInterceptor
{
    IFluentInterceptor Before(Action<IInvocation> before);
    IFluentInterceptor After(Action<IInvocation> after);
    IFluentInterceptor Finally(Action<IInvocation> @finally);
    IFluentInterceptor RunCondition(Func<IInvocation, bool> runCondition);
    IFluentInterceptor OnError(Func<IInvocation, Exception, bool> onError);
}

The FluentInterceptor type implements this. The Before, After, etc. methods are too simple to show; they all add to action queues that are meant to be used during method invocation, then each method returns this, allowing for method chaining.

The below code doesn't work, but I can't figure out why:

new ProxyGenerator().CreateClassProxyWithTarget(instance, new FluentInterceptor()
    .After(invocation =>
    {
        if (!invocation.Method.Name.StartsWith("set_")) return;
        string propertyName = invocation.Method.Name.Substring(4);
        FieldInfo info = invocation.TargetType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
            .Where(f => f.FieldType.Equals(typeof (PropertyChangedEventHandler)))
            .FirstOrDefault();
        if (info == null) return;
        var handler = info.GetValue(invocation.InvocationTarget) as PropertyChangedEventHandler;
        if (handler != null) handler.Invoke(invocation.TargetType, new PropertyChangedEventArgs(propertyName));
    }));

If I try that with CreateClassProxy, it works like a charm. Does anybody see what I'm doing wrong?

Thanks!

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

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

发布评论

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

评论(1

画骨成沙 2024-11-14 09:18:34

老实说,我认为这不是问题。如果我需要一个容器来为我生成 INotifyPropertyChanged 实现,那么可能会在边缘情况下插入由容器构建的模型对象,因为它们具有依赖性等在这些情况下,CreateClassProxy(拦截器) 就可以了。在所有其他情况下,我一直在使用 MVVM ViewModel,其中围绕更改通知的确切逻辑是根据具体情况决定的,重点是用户体验。模型类通常会以某种方式扁平化或转换,因此整个要点毫无意义。当我不明白如何在我的应用程序中正确利用 MVVM 时,我提出了这个问题,并且我认为我将负责更多的移动部分。一旦我开始正确配置我的 ViewModel,这实际上比预期容易得多。

Honestly, I think this is a non-issue. If I ever need a container to generate the INotifyPropertyChanged implementation for me, it'll probably be in fringe cases where I'm plugging in model objects that get built by the container anyway because they have depedencies, etc. In those cases CreateClassProxy<T>(interceptors) is fine. In all other cases I have been using MVVM ViewModels where the exact logic surrounding change notification is decided on a case-by-case basis, with the user experience being the focus. Model classes usually get flattened or transformed in some way, so the whole point is moot. I asked this question at a time when I didn't understand how to properly leverage MVVM in my app, and I thought I was going to be responsible for more moving parts. It's actually been a lot easier than anticipated, once I started configuring my ViewModels properly.

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