zoukankan      html  css  js  c++  java
  • AOP-SheepAspect

    转载https://www.cnblogs.com/InCsharp/p/5902133.html

    SheepAspect 简介以及代码示列:

    SheepAspect是一个AOP框架为.NET平台,深受AspectJ。它静织目标组件作为一个编译后的任务(编译时把AOP代码植入)。

    多有特性时,可根据参数值设置先后进入顺序

    下面开始代码实现之旅:

    一、新建控制台程序:方案名称:SheepAectTest

    二、NuGet上搜索SheepAspect进行安装

    三、安装完毕后的样子

    成员的切入点类型(SelectMethdos 以下图等): 

    "saql":

    Criteria Argument Examples
    Name string
    • Name: ‘*Customer’
    • Name: (‘*Service’ | ‘*Repository’)
    Namespace string
    • !Namespace: ‘System.*’
    ImplementsType Type Pointcut
    • ImplementsType:’System.Collections.IEnumerable’
    • ImplementsType: (Name: ‘*Repository’ | Namespace: ‘*.DataContexts’)
    AssignableToType Type Pointcut
    • AssignableToType:(‘System.Collections.*’ & Interface)
    • AssignableToType: Namespace: ‘System.Collections.*’
    HasMethod Method Pointcut
    • HasMethod: Name: ‘Get*’
    • HasMethod: (Public & Args(‘System.Int32’)
    HasProperty Property Pointcut
    • HasProperty:Name:’Length’
    • HasProperty:Type:Implements:’*.*Service’
    HasField Field Pointcut
    • HasField:Name:(‘_createdDate’ | ‘_entryDate’)
    • HasField:((Public & Static) | Protected)
    ThisAspect (none)
    • ThisAspect
    • Implements:ThisAspect
    • Namespace:’Sheep.*’ & !ThisAspect
    HasCustomAttributeType Type Pointcut
    • HasCustomAttributeType:ImplementsType:’BindableAttribute’
    InheritsType* Type Pointcut
    • InheritsType:Namespace:’System.Collections’
    Interface* (none)
    • Interface
    • !Interface
    Abstract* (none)
    • Abstract & Name:’*Strategy’
    ValueType* (none)
    • ValueType & HasMethod:Name:’Equals’
    Class* (none)
    • Class & Implements:’Sheep.Irepository’

    特性植入示列:

    一、新建特性

    复制代码
       public class LogAttribute:Attribute
        {
            public string Name { get; set; }
    
            public LogAttribute(string name)
            {
                Name = name;
            }
        }
    复制代码

    二、新建一个测试类TestClass.cs

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public class TestClass
    {
        [Log("获取的第一个方法")]
        public string Get()
        {
            return "test1";
        }
     
        public string Get2()
        {
            return "test2";
        }
    }

    三、更改SampleAspect为:HasCustomAttributeType:'SheepAectTest.Attr.LogAttribute'   >  命名空间+类名

    四:编写测试代码:

     输出结果:

     

    如果我们在AOP中更改结果呢?

     输出结果:

    获取特性的属性:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    [Aspect]
      public class SampleAspect
      {
          [SelectMethods("HasCustomAttributeType:'SheepAectTest.Attr.LogAttribute'")]
          public void PublicMethods() { }
     
          [Around("PublicMethods", Priority = 100)]
          public object LogAroundMethod(MethodJointPoint jp)
          {
     
              try
              {
                  var log = (LogAttribute)jp.Method.GetCustomAttributes(typeof(LogAttribute), false)[0];
                  //这样可以获取属性名称:log.Name;
                  //jp.Args  -> 包含传递参数
                  var result = jp.Execute();
     
                  if (jp.Method.ReturnType == typeof(void))
                      result = "{void}";
     
                  result = "AOP更改结果";
                  return result;
              }
              catch (Exception e)
              {
     
                  throw;
              }
          }
      }

      

    多个特性注入顺序以:Priority属性值控制优先级较低的值;

  • 相关阅读:
    QT:不规则窗口的实现
    Sql Server (错误:7302)
    Qt源码分析之信号和槽机制
    Linux下如何发布Qt程序
    QT程序制作deb包并安装在应用程序菜单
    Another Look at Events(再谈Events)
    Qt全局热键(windows篇)(使用RegisterHotKey和句柄进行注册)
    新浪微博回归平静
    Qt中 QString 和int, char等的“相互”转换,关键是QString.toLocal8Bit().data();
    QT中异形窗口的绘制(winEvent处理WM_NCHITTEST消息)
  • 原文地址:https://www.cnblogs.com/anyihen/p/12740144.html
Copyright © 2011-2022 走看看