闲来无事,做了一个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实现的更多相关文章

  1. 基于spring注解AOP的异常处理

    一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...

  2. Spring基于AOP的事务管理

                                  Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...

  3. 学习AOP之透过Spring的Ioc理解Advisor

    花了几天时间来学习Spring,突然明白一个问题,就是看书不能让人理解Spring,一方面要结合使用场景,另一方面要阅读源代码,这种方式理解起来事半功倍.那看书有什么用呢?主要还是扩展视野,毕竟书是别 ...

  4. 学习AOP之深入一点Spring Aop

    上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...

  5. 学习AOP之认识一下Spring AOP

    心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...

  6. .Net中的AOP系列之构建一个汽车租赁应用

    返回<.Net中的AOP>系列学习总目录 本篇目录 开始一个新项目 没有AOP的生活 变更的代价 使用AOP重构 本系列的源码本人已托管于Coding上:点击查看. 本系列的实验环境:VS ...

  7. .NET里简易实现AOP

    .NET里简易实现AOP 前言 在MVC的过滤器章节中对于过滤器的使用就是AOP的一个实现了吧,时常在工作学习中遇到AOP对于它的运用可以说是很熟练了,就是没想过如果自己来实现的话是怎么实现的,性子比 ...

  8. 在.Net中实现自己的简易AOP

    RealProxy基本代理类 RealProxy类提供代理的基本功能.这个类中有一个GetTransparentProxy方法,此方法返回当前代理实例的透明代理.这是我们AOP实现的主要依赖. 新建一 ...

  9. 使用Java原生代理实现AOP

    ### 本文由博主柒.原创,转载请注明出处 ### 完整源码下载地址 [https://github.com/MatrixSeven/JavaAOP](https://github.com/Matri ...

  10. 【开源】.Net Aop(静态织入)框架 BSF.Aop

    BSF.Aop .Net 免费开源,静态Aop织入(直接修改IL中间语言)框架,类似PostSharp(收费): 实现前后Aop切面和INotifyPropertyChanged注入方式. 开源地址: ...

随机推荐

  1. day10-高阶函数

    高阶函数 高阶函数:就是把函数当成参数传递的一种函数,例如: def add(x,y,f): return f(x)+f(y) print(add(-8,11,abs)) 结果: 19 解释: 调用a ...

  2. zookeeper集群环境搭建(使用kafka的zookeeper搭建zk集群)

    ---恢复内容开始--- 使用kafka的zookeeper来搞集群的话和单纯用zk的其实差不了多少. 0.说在前头,搭建kafka集群之前请把每个服务器的jdk搞起来. 1.安装kafka wget ...

  3. Mysql InnoDB三大特性-- change buffer

    Mysql InnoDB三大特性-- change buffer

  4. cv::ACCESS_MASK指定不明确的错误

    今天想实现在opencv下使用模拟按键,结果出现cv::ACCESS_MASK指定不明确的错误,查找得到如下原因: 在winnt.h里面有一个cv的命名空间,同样定义了一个ACCESS_MASK,跟o ...

  5. core net 2 nuget的数据源包

    基本都是 在 obj里面 debug里面 porgect.assetc.json

  6. 4.6 C++抽象基类和纯虚成员函数

    参考:http://www.weixueyuan.net/view/6376.html 总结: 在C++中,可以通过抽象基类来实现公共接口 纯虚成员函数没有函数体,只有函数声明,在纯虚函数声明结尾加上 ...

  7. 04 复制删除行为IDA反汇编

     (很久以前的学习记录,放到博客上来)   (IDA5.0版的不知道为何反汇编进去每一行被截断的景象,惨不忍睹......明明是个正版的.只好回来用拷过来的破解版,依然有一些叽里呱啦的问题,懒得管了, ...

  8. 运行时常量池中的符号引用/String.intern() /ldc指令

    运行时常量池,之前放在方法区(永久代)中,1.8之后被转移到元空间,放到了native memory中. 具体的数据结构是:(看对象的内存布局,句柄访问还是对象头中保存指向类的元数据的指针,这里以对象 ...

  9. jquery 正则表达式

  10. python 时间戳转换格式

    1.简介 在编写代码时,往往涉及时间.日期.时间戳的相互转换. 2.示例 # 引入模块 import time, datetime 2.1 str类型的日期转换为时间戳 1 # 字符类型的时间 2 t ...