zoukankan      html  css  js  c++  java
  • 用.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
  • 相关阅读:
    Linux内存管理(text、rodata、data、bss、stack&heap)
    名词解释:Linux内存管理之RSS和VSZ
    数据库对比:选择MariaDB还是MySQL?
    linux 下 pip 安装教程
    Mysqlbinlog工具及导出数据并转换编码导入
    运维监控系统之Open-Falcon
    MySQL binlog格式解析
    理解和配置Out of memory: Kill process
    mysql优化——explain详解
    mysql性能优化-慢查询分析、优化索引和配置 (慢查询日志,explain,profile)
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/6806958.html
Copyright © 2011-2022 走看看