闲来无事,做了一个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. JSP动态网页

    01.什么是服务器 02.什么是动态网页  动态网页是指在服务器端运行的,使用程序语言设计的交互式网页,它们会根据某种条件的变化,返回不同的网页内容.可以让用户和服务器交互的网站 动态网站可以实现交互 ...

  2. GTX使用(更新中)

    1.XILINX GTX介绍GTX是Virtex系列 FPGA上的低功耗吉比特收发器,在V6芯片上GTX工作带宽范围是750Mb/s到6.6Gb/s,支持收发双向,且收发双向独立.GTX接收和发送方向 ...

  3. matla互相关协方差的计算和理解

    计算相关函数和协方差的MATLAB函数 MATLAB信号处理工具箱提供了计算随机信号相关函数xcorr. 函数xcorr用于计算随机序列自相关和互相关函数.调用格式为: [c,lags]=xcorr( ...

  4. Java伪代码之大道至简读后感

    import.java.大道至简.*; import.java. 愚公移山.*; public class YuGongYiShan//定义一个名为YuGongYiShan的类 {//类定义的开始 S ...

  5. 20165326 java实验二

    2017-2018-2 20165326实验二<Java面向对象程序设计>实验报告 课程:Java程序设计 班级:1653班 姓名:陈卓 学号:20165326 成绩:指导教师:娄嘉鹏 实 ...

  6. Linux:NFS配置

    NFS配置 1.创建分享的文件:touch /var/www/html/aa.txt2.查看是否安装NFS:rpm -qa|grep nfs3.查看IP地址:ifconfig4.配置NFS:vi /e ...

  7. Oracle create tablespace 、create user and so on

    1.创建临时表空间 CREATE TEMPORARY TABLESPACE test_tempTEMPFILE 'C:\oracle\product\10.1.0\oradata\orcl\test_ ...

  8. vue 自定义过滤器 格式化金额(保留两位小数)

    1.js部分 import Vue from 'vue' Vue.filter('money', function(val) { val = val.toString().replace(/\$|\, ...

  9. C# 解压

    需要ICSharpCode.SharpZipLib.dll 网上有很多 先添加文件引用 再添加引用 using ICSharpCode.SharpZipLib.Zip; #region 解压 /// ...

  10. Java学习笔记29(IO字符流,转换流)

    字符流:只能操作文本文件,与字节流的区别是,字节流是按照字节来读取文件,而字符流是按照字符来读取,因此字符流的局限性为文本文件 字符输出流:Write类,使用时通过子类   每一次写入都要刷新 pac ...