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注入方式. 开源地址: ...
随机推荐
- bzoj1294
题解: 首先发现假如一个豆豆被多边形围住了,那么从这个豆豆引出一条射线 会有奇数个焦点 然后我们从每个豆豆引出一条射线 然后状压dfs 代码: #include<bits/stdc++.h> ...
- Java并发编程_wait/notify和CountDownLatch的比较(三)
1.wait/notify方法 package sync; import java.util.ArrayList; import java.util.List; public class WaitA ...
- QuickHit 项目
package cn.javaoppday01; import java.util.Random; public class Game { public Player player; public G ...
- 深入理解java虚拟机---java虚拟机内存管理(五)
1.深入理解java虚拟机 总图: 1.线程共享区: 2.线程独占区: 1.程序计数器 理解为当前线程锁执行的字节码的行号指示器,程序计数器没有内存异常错误.
- 1449 - The user specified as a definer('xxx'@'%') does not exist
指定的用户不存在,创建相应的账户即可,注意主机那里填的内容,我的这个是@'%'所以不用填任何内容.
- Oracle特殊字符转义:&和'
Oracle特殊字符转义:&和' 我们在SQL*PLUS下执行 SQL show all命令时,可以发现一个参数:define & (hex 26),如下所示 concat . ...
- 20165214 2017-2018-2 《Java程序设计》课程总结
20165214 2017-2018-2 <Java程序设计>课程总结 每周任务链接 预备作业1:我期望的师生关系 预备作业2:C语言基础调查和java学习展望 预备作业3:Linux安装 ...
- 字符界面的贪吃蛇--链表--C++
前天看了下链表,由于平时对链表的使用不多,所以对链表的应用也没什么了解,所以想来想去,就想用链表实现一下贪吃蛇. 下面言归正传,先看效果图,再看代码,其他没有了! 图1: 图2: 代码: #inclu ...
- [python]操作redis sentinel以及cluster
先了解清楚sentinel和cluster的差别,再学习使用python操作redis的API,感觉会更加清晰明白. 1.redis sentinel和cluster的区别 sentinel遵循主从结 ...
- Oralce 11g新特性 转载
Oracle 11g于2007年7月11日美国东部时间11时(北京时间11日22时)正式发布,11g是甲骨文公司30年来发布的最重要的数据库版本,根据用户的需求实现了信息生命周期管理(Informat ...