用.net4中的DynamicObject实现简单AOP

 public class DynamicWrapper : DynamicObject
    {
        private readonly object source;
 
        public DynamicWrapper(object source)
        {
            this.source = source;
        }
 
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            var methodInfo = source.GetType().GetMethod(binder.Name);
            if (methodInfo != null)
            {
                Func<object, object[], object> func = (s, a) => methodInfo.Invoke(s, a);
 
                result = MethodCall(func, source, args);
 
                return true;
            }
 
            result = null;
 
            return false;
        }
 
        protected virtual object MethodCall(Func<object, object[], object> func, object src, object[] args)
        {
            return func(src, args);
        }
    }

  

  public class TryCatchDynamicWrapper : DynamicWrapper

    {

        public TryCatchDynamicWrapper(object source)

            : base(source)

        { }

 

        protected override object MethodCall(Func<object, object[], object> func, object src, object[] args)

        {

            try

            {

                return base.MethodCall(func, src, args);

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex);

                throw;

            }

 

        }

    }

  码是这样的,这里我们增一个扩展方法,这样使我们的代码更加简洁,您会在后面的Unit Test中看到:

   1:      public interface ITestObject
   2:      {
   3:          int Div(int num1, int num2);
   4:      }
   5:   
   6:      public class TestObject : ITestObject
   7:      {
   8:   
   9:          #region ITestObject Members
  10:   
  11:          public int Div(int num1,int num2)
  12:          {
  13:              return num1 / num2;
  14:          }
  15:   
  16:          #endregion
  17:   
  18:          public ITestObject WrapDynamicConsoleLogging
  19:          {
  20:              get
  21:              {
  22:                  return this.WithMethodConsoleLogging();
  23:              }
  24:          }
  25:      }
  26:   
  27:      public static class Extenstions
  28:      {
  29:          public static dynamic WithMethodConsoleLogging(this ITestObject testobject)
  30:          {
  31:              return new DynamicLoggingWrapper(Activator.CreateInstance<TestObject>(), ConsoleLogger.Instance);
  32:          }
  33:   
  34:          public static dynamic WrapWith<T>(this ITestObject testobject) where T : DynamicWrapper
  35:          {
  36:              return Activator.CreateInstance(typeof(T), new object[] { Activator.CreateInstance<TestObject>() });
  37:          }
  38:      }

 

 

Unit Test,如何使用呢:

   1:          [Test]
   2:          [ExpectedException(typeof(TargetInvocationException))]
   3:          public void TestTryCatchDynamicWrapper()
   4:          {
   5:              dynamic proxy = new TryCatchDynamicWrapper(new TestObject());
   6:              var ll = proxy.Div(6, 0);
   7:          }
   8:   

http://www.cnblogs.com/wintersun/archive/2011/06/19/2084755.html
posted @ 2017-05-04 14:30  hi.....  阅读(232)  评论(0编辑  收藏  举报