.NET中AOP方便之神SheepAspect
来源:互联网 发布:中国网络研究院 待遇 编辑:程序博客网 时间:2024/05/19 17:56
SheepAspect 简介以及代码示列:
SheepAspect是一个AOP框架为.NET平台,深受AspectJ。它静织目标组件作为一个编译后的任务(编译时把AOP代码植入)。
多有特性时,可根据参数值设置先后进入顺序
下面开始代码实现之旅:
一、新建控制台程序:方案名称:SheepAectTest
二、NuGet上搜索SheepAspect进行安装
三、安装完毕后的样子
成员的切入点类型(SelectMethdos 以下图等):
"saql":
- Name: ‘*Customer’
- Name: (‘*Service’ | ‘*Repository’)
- !Namespace: ‘System.*’
- ImplementsType:’System.Collections.IEnumerable’
- ImplementsType: (Name: ‘*Repository’ | Namespace: ‘*.DataContexts’)
- AssignableToType:(‘System.Collections.*’ & Interface)
- AssignableToType: Namespace: ‘System.Collections.*’
- HasMethod: Name: ‘Get*’
- HasMethod: (Public & Args(‘System.Int32’)
- HasProperty:Name:’Length’
- HasProperty:Type:Implements:’*.*Service’
- HasField:Name:(‘_createdDate’ | ‘_entryDate’)
- HasField:((Public & Static) | Protected)
- ThisAspect
- Implements:ThisAspect
- Namespace:’Sheep.*’ & !ThisAspect
- HasCustomAttributeType:ImplementsType:’BindableAttribute’
- InheritsType:Namespace:’System.Collections’
- Interface
- !Interface
- Abstract & Name:’*Strategy’
- ValueType & HasMethod:Name:’Equals’
- 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属性值控制优先级较低的值;
作者:疯狂的果子
来源:http://incsharp.cnblogs.com/
声明:原创博客请在转载时保留原文链接或者在文章开头加上本人博客地址,如发现错误,欢迎批评指正。如有特殊需求请与本人联系!
1 0
- .NET中AOP方便之神SheepAspect
- Spring.Net之Aop
- 在ASP.NET中方便重写URL
- 底层之Spring.net AOP
- .net中使用spring.net实现aop
- .Net中的AOP系列之《AOP实现类型》
- AOP系列:.NET中AOP的几种实现方案
- Spring之AOP使用xml配置更方便(使用AspectJ框架)(重点)
- 在.Net中关于AOP的实现
- .NET中使用unity实现aop
- .net中使用postsharp来实现aop
- SSH框架中使用log4j的方便之处
- 在ACM中使用Java的若干方便之处
- SSH框架中使用log4j的方便之处
- AOP之AOP基础知识
- 什么是AOP? AOP .Net 框架,AOP 资源
- 在.Net中关于AOP的实现 (一)
- 在.Net中关于AOP的实现(二)
- android 6.0权限
- 简单理解滤波器(入门经典)
- 错排+组合——HDU 2049
- Android中自定义view
- Broadcast详解(有序广播,无序广播)最终广播,Bundle传递参数
- .NET中AOP方便之神SheepAspect
- 背包问题(递归算法)
- protected、try...finally
- 实现广告自动播放切换的效果
- 6个一点点送给自己``
- ("").equals(str)与str.equals("")的区别
- poj 1753Flip Game(dfs)
- AFNetworking 3 批量上传图片的 3 种方法
- Redis内存footprint