.net有没有类似java的动态代理的机制呢?或者类似spring之类的框架呢?.netaopspring

解决方案 »

  1.   

    即使.net框架没自带,也可以自己实现http://www.codeproject.com/search.aspx?q=aop&x=0&y=0&sbo=kw
      

  2.   

    aop其实是华而不实的东西,.net有替代它的机制,
      

  3.   

    和Java这种早期落后的语言相比,C# 4.0开始支持真正意义上的动态语法。下面是一个动态拦截属性是否被修改的例子代码: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);
            }
        }
    }