处理PropertyChanging /通过城堡的DynamicProxy的PropertyChanged [英] Handling PropertyChanging/PropertyChanged via Castle's DynamicProxy

查看:121
本文介绍了处理PropertyChanging /通过城堡的DynamicProxy的PropertyChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个setter方法​​,看起来是这样的:

I currently have a setter method which looks like this:

 private string _a;
    public virtual string A
    {
        get { return _a; }
        set
        {
            if (_a!= value)
            {
                if (this.OnPropertyChanging("A", _a, value))
                {
                    var previousValue = _a;
                    _a = value;
                    this.OnPropertyChanged("A", previousValue, value);
                }
            }

        }
    }

我已经实现了这个与威利博士的鸭prentice帮助(http://stackoverflow.com/a/8578507/981225),带有自定义更改处理程序,跟踪旧的和当前值,以及以设定的变迁事件为取消的能力,使得所述的PropertyChange不会发生

I have implemented this with help from Dr Wily's Apprentice (http://stackoverflow.com/a/8578507/981225), with a custom Changing handler that keeps track of the old and current value, as well as the ability to set the Changing event as 'Cancelled', such that the PropertyChange will not occur.

这完美的作品。但是,我们有数百个属性,这是一个很大$ C C重复$的。

This works perfectly. However, we have hundreds of properties and this is a lot of code the duplicate.

我已经使用了城堡的DynamicProxy过去,以避免写'OnPropertyChanged(A)。

I have used Castle's DynamicProxy in the past, to avoid having to write 'OnPropertyChanged("A")'.

我怎样才能实现这个二传中的逻辑,作为代理的拦截方法的一部分?可能吗?谢谢你。

How can I implement the logic within this setter, as part of the Proxy's Intercept method? Is it possible? Thank you.

推荐答案

也许我是有点晚了,但我在LINQ到SharePoint的模式遇到了一个类似的任务。我勾画了一些code。如果有一个人仍然不知道。

Maybe I'm a bit late, but I came across a similar task in the Linq-To-SharePoint model. I sketched some code if some one's still wondering.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;
using System.Reflection;
using System.ComponentModel;


namespace DemoSpace
{
    public abstract class EntityBase : INotifyPropertyChanged, INotifyPropertyChanging
    {
        public virtual void OnPropertyChanging(string propertyName, object value)
        {
            if ((null != PropertyChanging))
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        public virtual void OnPropertyChanged(string propertyName)
        {
            if ((null != PropertyChanged))
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler  PropertyChanged;

        public event PropertyChangingEventHandler  PropertyChanging;
    }

    public class DemoInterceptor<T> : IInterceptor where T : EntityBase
    {
        private T _target;

        public DemoInterceptor(T target)
        {
            _target = target;
        }

        public void Intercept(IInvocation invocation)
        {
            if (invocation.Method.IsPublic && invocation.Method.Name.StartsWith("set_"))
            {
                string propertyName = invocation.Method.Name.Substring(4);
                string privateFieldName = ResolvePropName(propertyName); 


                object original_value = 
                    typeof(T).GetField(privateFieldName, BindingFlags.NonPublic | BindingFlags.Instance).GetValue(_target);

                _target.OnPropertyChanging(propertyName, original_value);
                invocation.Method.Invoke(_target, invocation.Arguments);
                _target.OnPropertyChanged(propertyName);

            }
            else
            {
                invocation.ReturnValue = invocation.Method.Invoke(_target, invocation.Arguments);
            }

        }

        public virtual string ResolvePropName(string propertyName)
        {
            return "_" + propertyName.Substring(0, 1).ToLower() + propertyName.Substring(1);
        }

    }
}

这篇关于处理PropertyChanging /通过城堡的DynamicProxy的PropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆