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

    public class TestClass
{
[Log("获取的第一个方法")]
public string Get()
{
return "test1";
} public string Get2()
{
return "test2";
}
}

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

四:编写测试代码:

输出结果:

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

输出结果:

获取特性的属性:

  [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/ 
声明:原创博客请在转载时保留原文链接或者在文章开头加上本人博客地址,如发现错误,欢迎批评指正。如有特殊需求请与本人联系!

433685124QQ群

.NET中AOP方便之神SheepAspect的更多相关文章

  1. Spring中AOP简介与切面编程的使用

    Spring中AOP简介与使用 什么是AOP? Aspect Oriented Programming(AOP),多译作 "面向切面编程",也就是说,对一段程序,从侧面插入,进行操 ...

  2. Spring中AOP原理,源码学习笔记

    一.AOP(面向切面编程):通过预编译和运行期动态代理的方式在不改变代码的情况下给程序动态的添加一些功能.利用AOP可以对应用程序的各个部分进行隔离,在Spring中AOP主要用来分离业务逻辑和系统级 ...

  3. 第四节:MVC中AOP思想的体现(四种过滤器)并结合项目案例说明过滤器的实际用法

    一. 简介 MVC中的过滤器可以说是MVC框架中的一种灵魂所在,它是MVC框架中AOP思想的具体体现,所以它以面向切面的形式无侵入式的作用于代码的业务逻辑,与业务逻辑代码分离,一经推出,广受开发者的喜 ...

  4. 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)

    一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...

  5. Spring中AOP实现

    1.什么是SpringAOP 什么是aop:Aspect Oriented Programming的缩写,面向切面编程,通过预编译和动态代理实现程序功能的 统一维护的一种技术 主要功能:日志记录,性能 ...

  6. Spring 中aop切面注解实现

    spring中aop的注解实现方式简单实例   上篇中我们讲到spring的xml实现,这里我们讲讲使用注解如何实现aop呢.前面已经讲过aop的简单理解了,这里就不在赘述了. 注解方式实现aop我们 ...

  7. Spring中AOP相关源码解析

    前言 在Spring中AOP是我们使用的非常频繁的一个特性.通过AOP我们可以补足一些面向对象编程中不足或难以实现的部分. AOP 前置理论 首先在学习源码之前我们需要了解关于AOP的相关概念如切点切 ...

  8. AOP 与 Spring中AOP使用(上)

    AOP简介 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程, 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP是OOP的延续 ...

  9. Spring中AOP的基于xml开发和配置

    pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

随机推荐

  1. C#异步编程(一)

    异步编程简介 前言 本人学习.Net两年有余,是第一次写博客,虽然写的很认真,当毕竟是第一次,肯定会有很多不足之处, 希望大家照顾照顾新人,有错误之处可以指出来,我会虚心接受的. 何谓异步 与同步相对 ...

  2. SQL Server相关书籍

    SQL Server相关书籍 (排名不分先后) Microsoft SQL Server 企业级平台管理实践 SQL Server 2008数据库技术内幕 SQL Server性能调优实战 SQL S ...

  3. 【腾讯bugly干货分享】HTML 5 视频直播一站式扫盲

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=1277 视频直 ...

  4. POCO Controller 你这么厉害,ASP.NET vNext 知道吗?

    写在前面 阅读目录: POCO 是什么? 为什么会有 POJO? POJO 的意义 POJO 与 PO.VO 的区别 POJO 的扩展 POCO VS DTO Controller 是什么? 关于 P ...

  5. 【SQLServer】记一次数据迁移-标识重复的简单处理

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 今天在数据迁移的时候因为手贱遇到一个坑爹问题,发来大家乐乐,也传授新手点经验 迁移惯用就 ...

  6. C# 多种方式发送邮件(附帮助类)

    因项目业务需要,需要做一个发送邮件功能,查了下资料,整了整,汇总如下,亲测可用- QQ邮箱发送邮件 #region 发送邮箱 try { MailMessage mail = new MailMess ...

  7. Android Retrofit 2.0 使用-补充篇

    推荐阅读,猛戳: 1.Android MVP 实例 2.Android Retrofit 2.0使用 3.RxJava 4.RxBus 5.Android MVP+Retrofit+RxJava实践小 ...

  8. CentOS:设置系统级代理(转)

    原文地址:http://www.cnblogs.com/cocowool/archive/2012/07/05/2578487.html YUM代理设置 编辑/etc/yum.conf,在最后加入 # ...

  9. Linux初识

    在这篇文章中你讲看到如下内容: 计算机的组成及功能: Linux发行版之间的区别和联系: Linux发行版的基础目录及功用规定: Linux系统设计的哲学思想: Linux系统上获取命令帮助,及man ...

  10. 使用CocosSharp制作一个游戏 - CocosSharp中文教程

    注:本教程翻译自官方<Walkthrough - Building a game with CocosSharp>,官方教程有很多地方说的不够详细,或者代码不全,导致无法继续,本人在看了G ...