Unlimited Technique          Unlimited Wisdom

1001010000101001000010010001000010000101101110111 1101010010010101011110010101110101011010101010101

导航

AOP framework for .net – Future.AOP

 

AOP framework for .net – Future.AOP

设置文件描述

文件分为两部分

1、  pointcuts(关注点)

2、  advice(建议)

 

pointcuts设置

4pointcutscallgetsetcallconstructor

call: 所有的public方法或protected方法调用(必须加virtual keyword)

get: 得到属性值

set: 设置属性值

callconstructor: 构造函数的调用

现举例call的设置

<call enabled="true">

<returnvalue type = "System.String"/>

  <callingclass type = "any" path = ""/>

  <methodname value="GetMy"  valuetype="left"  /> 

  <arguments  count = "-1"> 

     <argument type="System.Int32" />

     <argument type="any" />

  </arguments>                

</call>

 

call必须小写,其它pointcut也是一样。enabledtrue 这个pointcut生效,  false 这个pointcut失效

call4个子项

returnvalue : 方法的返回值,如果是内部类型,可直接写System.Type, System.Int32, System.Char等,如果是自定义的,需要加上path, 如方法返回一个Test.Class1类型,这样写<returnvalue type=”Test.Class1” path = “AAA.exe”/>  AAA.exeTest.Class1所在的Assembly,也可以是*.dll。如是返回是void, type处可直接写type = “void”

callingclass:这个调用方法所在的类,写法和returnvalue一样,如果想所有类中的某个特定的方法都匹配,type “any”, path “”

methodname要匹配的方法名。Valuetype4个值,left,right,middle,full

            left  这个名字左侧和value匹配。

            right:   这个名字右侧和value匹配。

            middle: 这个名字中间的value匹配

            full:    完全匹配(default)

arguments:这个方法的参数  count 为参数的个数。

argument:每个参数的类型,可以是自定义的。

如果count=-1,那么方法的参数只匹配argument的实际数量。如

以上xml,有方法 public virtual String GetMyMethod(Int32 x, String s, Single a)

这个函数的第三个参数忽略,因此这是匹配的。

其它三个pointcutcal类似,请见debug目录中的sample.xml

还有一个关系结点,<relation value = "or" />  sample.xml, value也可为and,是设置这4pointcut的关系的。

另外这4pointcut是包含在一个pointcutgroup中的,这个pointcutgroup有一个唯一的名字,见sample.xml

 

advice设置

<advice>

<assemblyfile path = "FutureAOP_Sample.exe"> 设置要进行拦截的类所在的assembly

    <aspectclass type="FutureAOP_Sample.MyAspect1">  拦截类

       <actions >               

         <before enabled = "false"/>        在要拦截方法(属性、构造函数)之前执行

         <after enabled = "false"/>         在要拦截方法(属性、构造函数)之后执行

         <around enabled = "true"/>        取代要拦截的函数  

       </actions>

       <pointcuts>

          <pointcutgroupname value = "p1"/>          满足这些pointcut的所有方法(属性、构造函数)执行以上操作。

           <relation value = "or" />   -- default value is or

           <pointcutgroupname value = "p2"/>       

        </pointcuts>

     </aspectclass>

  </assemblyfile>

</advice>

详细设置见sample.xmldemo

别外提一句,如果你想要拦截方法、属性,FutureAOP_Sample.MyAspect1需要实现IMethodAdvice接口或从DefaultAdvice继承,如果要拦截构造函数,需要IConstructorAdvice实现或从DefaultAdvice继承。见demo

 

以后我会提供一个tool来设置这个xml文件。

Demo下载:https://files.cnblogs.com/bughole/FutureAOP_Sample.rar

 必须在visual studio 2005下打开demo,可在.net framework2.0下运行程序。

posted on 2005-07-28 15:14  bughole  阅读(1148)  评论(5编辑  收藏  举报