C# 的AOP实现
闲来无事,做了一个AOP示例,此示例只能捕获方法调用事件,无法动态阻止方法调用的执行。因为取消后构造返回值成了难题,返回null貌似会报错。如果不需要这个功能,其实还是很完美的。
缺点是没有以接口方式实现,使用类必须继承ContextBoundObject。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Messaging; namespace Csharp实现AOP方法拦截
{
#region AOP Helper public class MyApoAspect : IMessageSink
{
private IMessageSink _NextSink = null;
private IMyAopMethodFilter _MethodFilter; internal MyApoAspect(IMessageSink msgSink, IMyAopMethodFilter methodFilter)
{
_NextSink = msgSink;
_MethodFilter = methodFilter;
} public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
{
IMessage message = this.SyncProcessMessage(msg);
replySink.SyncProcessMessage(message);
return null; } public IMessageSink NextSink
{
get { return _NextSink; }
} public IMessage SyncProcessMessage(IMessage msg)
{
var cancel = false;
_MethodFilter.PreAopMethodFilter(msg,ref cancel);
if (cancel)
{
//此处不知如何返回
return null;
}
IMessage returnMethod = _NextSink.SyncProcessMessage(msg);
_MethodFilter.PostAopMethodFilter(msg);
return returnMethod;
}
} public class MyAopProperty: IContextProperty, IContributeObjectSink
{
private IMyAopMethodFilter _MethodFilter; public MyAopProperty(IMyAopMethodFilter methodFilter)
{
_MethodFilter = methodFilter;
} public void Freeze(Context newContext) { } public bool IsNewContextOK(Context newCtx)
{
return true;
} public string Name
{
get
{
return "MyAopProperty";
}
} public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink)
{
return new MyApoAspect(nextSink, _MethodFilter);
}
} [AttributeUsage(AttributeTargets.Class)]
public class MyAopAttribute : ContextAttribute, IMyAopMethodFilter
{
private IMyAopMethodFilter _MethodFilter; public MyAopAttribute()
: base("MyAopAttribute")
{
_MethodFilter = this;
} public override void GetPropertiesForNewContext(IConstructionCallMessage ccm)
{
ccm.ContextProperties.Add(new MyAopProperty(_MethodFilter));
} protected virtual void OnPreAopMethodFilter(IMessage msg, ref bool Cancel)
{
throw new NotImplementedException();
} protected virtual void OnPostAopMethodFilter(IMessage msg)
{
throw new NotImplementedException();
} public void PreAopMethodFilter(IMessage msg ,ref bool Cancel)
{
this.OnPreAopMethodFilter(msg,ref Cancel);
} public void PostAopMethodFilter(IMessage msg)
{
this.OnPostAopMethodFilter(msg);
}
} public interface IMyAopMethodFilter
{
void PreAopMethodFilter(IMessage msg, ref bool Cancel);
void PostAopMethodFilter(IMessage msg);
} #endregion #region AOP Demo public class Class1AopAttribute : MyAopAttribute
{
protected override void OnPreAopMethodFilter(IMessage msg, ref bool Cancel)
{
IMethodMessage call = msg as IMethodMessage;
Type type = Type.GetType(call.TypeName);
string callStr = type.Name + "." + call.MethodName;
var args = call.Args;
Console.WriteLine("在执行前拦截到" + callStr);
} protected override void OnPostAopMethodFilter(IMessage msg)
{
IMethodMessage call = msg as IMethodMessage;
Type type = Type.GetType(call.TypeName);
string callStr = type.Name + "." + call.MethodName;
var args = call.Args;
Console.WriteLine("在执行后拦截到" + callStr);
}
} [Class1AopAttribute()]
public class Class1:ContextBoundObject
{
public int A { get; set; } public int B(ref int p)
{
p++;
return p + ;
}
} class Program
{
static void Main(string[] args)
{
Class1 c1 = new Class1();
c1.A = ; int p = ;
c1.B(ref p); Console.ReadLine();
}
} #endregion
}
C# 的AOP实现的更多相关文章
- 基于spring注解AOP的异常处理
一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...
- Spring基于AOP的事务管理
Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...
- 学习AOP之透过Spring的Ioc理解Advisor
花了几天时间来学习Spring,突然明白一个问题,就是看书不能让人理解Spring,一方面要结合使用场景,另一方面要阅读源代码,这种方式理解起来事半功倍.那看书有什么用呢?主要还是扩展视野,毕竟书是别 ...
- 学习AOP之深入一点Spring Aop
上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...
- 学习AOP之认识一下Spring AOP
心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...
- .Net中的AOP系列之构建一个汽车租赁应用
返回<.Net中的AOP>系列学习总目录 本篇目录 开始一个新项目 没有AOP的生活 变更的代价 使用AOP重构 本系列的源码本人已托管于Coding上:点击查看. 本系列的实验环境:VS ...
- .NET里简易实现AOP
.NET里简易实现AOP 前言 在MVC的过滤器章节中对于过滤器的使用就是AOP的一个实现了吧,时常在工作学习中遇到AOP对于它的运用可以说是很熟练了,就是没想过如果自己来实现的话是怎么实现的,性子比 ...
- 在.Net中实现自己的简易AOP
RealProxy基本代理类 RealProxy类提供代理的基本功能.这个类中有一个GetTransparentProxy方法,此方法返回当前代理实例的透明代理.这是我们AOP实现的主要依赖. 新建一 ...
- 使用Java原生代理实现AOP
### 本文由博主柒.原创,转载请注明出处 ### 完整源码下载地址 [https://github.com/MatrixSeven/JavaAOP](https://github.com/Matri ...
- 【开源】.Net Aop(静态织入)框架 BSF.Aop
BSF.Aop .Net 免费开源,静态Aop织入(直接修改IL中间语言)框架,类似PostSharp(收费): 实现前后Aop切面和INotifyPropertyChanged注入方式. 开源地址: ...
随机推荐
- 深入研究sqlalchemy连接池
简介: 相对于最新的MySQL5.6,MariaDB在性能.功能.管理.NoSQL扩展方面包含了更丰富的特性.比如微秒的支持.线程池.子查询优化.组提交.进度报告等. 本文就主要探索MariaDB当中 ...
- 四:FAQ附录(容器交互,镜像交互,镜像导出)
1.交互式运行容器的方法: 1>-it进入到操作系统中: 2>另开一个cmd验证: 3>这是在image之上多了一个可写的从:可以运行centos的命令做一些事(touch .yum ...
- Cracking The Coding Interview 1.6
//原文: // // Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, w ...
- CentOS7调整home盘空间到根目录
1:解除挂载 umount /home 如报错: [root@zabbix-hk-01 home]# umount /home umount: /home:目标忙. (有些情况下通过 lsof(8) ...
- Grid Layout 注释
.wrapper { display: grid;/*定义一个块级的网格容器*/ grid-template-columns: repeat(3, 1fr); /*表示平均分为3列*/ grid ...
- AMR11A - Magic Grid
Thanks a lot for helping Harry Potter in finding the Sorcerer's Stone of Immortality in October. Did ...
- drf 分页
分页: 1.简单的分页: 每页显示条数: page_size = api_settings.PAGE_SIZE 查询的页码数: page_query_param = "page" ...
- 2019-03-14-day010-函数进阶
昨日回顾 1.函数: 函数的定义 函数的参数 位置参数 关键字参数 混合参数 形参的位置上(默认参数) 实参的位置上(关键字参数) 位置参数 > 默认参数 三元运算符: c = a if a&g ...
- Eclipse_设置_01_自动提示
- 【Python】Excel-3
1. 导入Excel模块:from openpyxl import Workbook 2. 创建Excel对象:wb=Workbook() 3. 创建sheet:ws1=wb.create_sheet ...