Castle.core AOP

使用

 1  //代理生成器
 2  ProxyGenerator generator = new ProxyGenerator();
 3  //拦截器
 4  MyInterceptor interceptor = new MyInterceptor();
 5  //代理生成器使用拦截器生成代理对象
 6  IDBHelper dbhelper = generator.CreateClassProxy<MySqlDBHelper>(interceptor);
 7  Console.WriteLine("生成的类型时:{0}", dbhelper.GetType());
 8  int[] c = new int[3];
 9  c[0] = 1;
10  c[1] = 2;
11  c[2] = 3;
12  int c1 = 2;
13  dbhelper.show(2,3,c,ref c1);
14  dbhelper.show1();

其他

  1  public interface IDBHelper
  2  {
  3      void show(int a,int b, int[] c, ref int c1);
  4      void show1();
  5  }
  6  public class MySqlDBHelper : IDBHelper
  7  {
  8      public virtual void show(int a, int b, int[] c,ref int c1)
  9      {
 10          Console.WriteLine("I am Mysql");
 11      }
 12      public virtual void show1()
 13      {
 14          Console.WriteLine("I am Mysql1");
 15      }
 16  }
 17 
 18   
 19  public class MyInterceptor : StandardInterceptor
 20  {
 21      /// <summary>
 22      /// 调用前拦截
 23      /// </summary>
 24      /// <param name="invocation"></param>
 25      protected override void PreProceed(IInvocation invocation)
 26      {
 27 
 28          var parameters = invocation.Method.GetParameters();
 29 
 30          for (int i = 0; i < parameters.Length; i++)
 31          {
 32              var paramInfo = parameters[i];
 33              object argValue = invocation.Arguments[i];
 34 
 35              // 对参数值进行友好字符串转换,特别是处理数组
 36              string stringValue = ConvertArgumentToString(argValue);
 37              Console.WriteLine($"  参数 [{i}]: 名称={paramInfo.Name}, 类型={paramInfo.ParameterType.Name}, 值={stringValue}");
 38          }
 39          Console.WriteLine("调用前拦截,调用方法:" + invocation.Method.Name+$"参数:{ string.Join(", ", invocation.Arguments)} ");
 40 
 41 
 42          base.PerformProceed(invocation);
 43      }
 44      /// <summary>
 45      /// 将参数值转换为友好的字符串表示,特别处理数组类型。
 46      /// </summary>
 47      private string ConvertArgumentToString(object argument)
 48      {
 49          if (argument == null) return "null";
 50 
 51          Type argType = argument.GetType();
 52 
 53          // 处理数组(如 double[])
 54          if (argType.IsArray)
 55          {
 56              //try
 57              //{
 58              //    return JsonSerializer.Serialize(argument);
 59              //}
 60              //catch
 61              {
 62                  return $"[{string.Join(", ", (argument as System.Array).Cast<object>())}]";
 63              }
 64          }
 65          return argument.ToString();
 66 
 67          //// 处理简单类型
 68          //if (argType.IsPrimitive || argType == typeof(string))
 69          //{
 70          //    return argument.ToString();
 71          //}
 72 
 73          //// 对于其他复杂对象,使用序列化
 74          //try
 75          //{
 76          //    return JsonSerializer.Serialize(argument, new JsonSerializerOptions { WriteIndented = false });
 77          //}
 78          //catch
 79          //{
 80          //    return argument.ToString();
 81          //}
 82      }
 83      /// <summary>
 84      /// 拦截的方法返回时调用
 85      /// </summary>
 86      /// <param name="invocation"></param>
 87      protected override void PerformProceed(IInvocation invocation)
 88      {
 89          Console.WriteLine("调用方法返回时拦截,调用方法:" + invocation.Method.Name);
 90          base.PostProceed(invocation);
 91      }
 92      /// <summary>
 93      /// 调用后拦截
 94      /// </summary>
 95      /// <param name="invocation"></param>
 96      protected override void PostProceed(IInvocation invocation)
 97      {
 98          Console.WriteLine("调用后拦截,调用方法:" + invocation.Method.Name);
 99          base.PreProceed(invocation);
100      }

 

posted @ 2025-11-28 14:49  家煜宝宝  阅读(6)  评论(0)    收藏  举报