怎么设计一个类,能够监视这个类的任何属性是否被修改过(不需要修改所有属性的set函数的情况下)

解决方案 »

  1.   

    咳,还有后面的条件"(不需要修改所有属性的set函数的情况下)",我是因为觉得全都修改太麻烦了,属性太多,才有这个限定,如果实现方法比这个复杂的话,就没必要了.
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Dynamic;
    using System.Reflection;namespace ConsoleApplication1
    {
        class EntityNotifier<T> : DynamicObject where T : new()
        {
            public class ObjectChangedEventArgs : EventArgs
            {
                public string PropertyName { get; set; }
                public object OldValue { get; private set; }
                public ObjectChangedEventArgs(string name, object value)
                {
                    PropertyName = name;
                    OldValue = value;
                }
            }        private T innerObject { get; set; }        public EntityNotifier()
            {
                innerObject = new T();
            }        public EntityNotifier(T obj)
            {
                innerObject = obj;
            }        public static implicit operator EntityNotifier<T>(T obj)
            {
                return new EntityNotifier<T>(obj);
            }        public static implicit operator T(EntityNotifier<T> obj)
            {
                return obj.innerObject;
            }        public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                var p = typeof(T).GetProperties().SingleOrDefault(x => x.Name == binder.Name);
                if (p == default(PropertyInfo))
                {
                    result = null;
                    return false;
                }
                else
                {
                    result = p.GetValue(innerObject, new object[] { });
                    return true;
                }
            }        public override bool TrySetMember(SetMemberBinder binder, object value)
            {
                var p = typeof(T).GetProperties().SingleOrDefault(x => x.Name == binder.Name);
                if (p == default(PropertyInfo))
                {
                    return false;
                }
                else
                {
                    object oldvalue = p.GetValue(innerObject, new object[] { });
                    p.SetValue(innerObject, value, new object[] { });
                    if (ObjectChanged != null) ObjectChanged(this, new ObjectChangedEventArgs(binder.Name, oldvalue));
                    return true;
                }
            }        public event EventHandler<EntityNotifier<T>.ObjectChangedEventArgs> ObjectChanged;
        }    class Person
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }    class Program
        {
            static void Main(string[] args)
            {
                dynamic person = new EntityNotifier<Person>();
                person.ObjectChanged += new EventHandler<EntityNotifier<Person>.ObjectChangedEventArgs>((x, y) => 
                {
                    Console.WriteLine("obj.{0} changed.", y.PropertyName);
                });
                person.Name = "张三";
                person.Name = "李四";
                person.ID = 1;
                Person p = person;
                Console.WriteLine(p.Name);
            }
        }
    }
      

  3.   


    我能说项目用的是.NET 3.5吗?
    我真的不是故意的,如果明天都没有结果就结贴吧.
      

  4.   

    全部申明为virtual ,然后用动态代理类 做属性拦截具体方法google“C# AOP 动态代理 属性拦截”包括9楼的代码其实也是类似思想,先弄一个代理类出来,在代理类里做处理
      

  5.   


    用Emit也可以,Emit在.NET 1.1就存在了,不过用起来非常繁琐,有兴趣自己研究下吧。
      

  6.   

    感谢 devmiao ,你说的Emit正是我想要的!!!