.Net平台AOP技术研究

简单实现

通过继承实现

public interface ICoding
{
void DoSth();
}
public class Coding : ICoding
{
public virtual void DoSth()
{
Console.WriteLine("敲代码咯!");
}
}
public class ProjectDevelopment : Coding, ICoding
{
public override void DoSth()
{
before();
base.DoSth();
after();
} private void before()
{
Console.WriteLine("需求分析!");
}
private void after()
{
Console.WriteLine("测试!");
}
}

  

装饰者(由继承改作组合)

public interface ICoding
{
void DoSth();
}
public class Coding : ICoding
{
public virtual void DoSth()
{
Console.WriteLine("敲代码咯!");
}
}
public class ProjectDevelopment : ICoding
{
private ICoding coding;
public ProjectDevelopment(ICoding coding)
{
this.coding = coding;
}
public void DoSth()
{
before();
coding.DoSth();
after();
} private void before()
{
Console.WriteLine("需求分析!");
}
private void after()
{
Console.WriteLine("测试!");
}
}

  

ASP.NET MVC中的拦截器

public interface IAdditionalOp
{
void before();
void after();
}
public class AdditionalOp : Attribute, IAdditionalOp
{
public void before()
{
Console.WriteLine("需求分析!");
}
public void after()
{
Console.WriteLine("测试!");
}
} public class Coding
{
[AdditionalOp]
public void DoSth()
{
Console.WriteLine("敲代码咯!");
}
} public class ProjectDevelopment
{
public void Development()
{
var classUnder = typeof(Coding);
var allMethods = classUnder.GetMethods();
var methodUnder = allMethods.Where(m => m.Name == "DoSth"); foreach (MethodInfo methodInfo in methodUnder)
{
Attribute[] attributes = Attribute.GetCustomAttributes(methodInfo, typeof(AdditionalOp));
foreach (var item in attributes)
{
(item as AdditionalOp).before();
} Coding coding = new Coding();
coding.DoSth(); foreach (var item in attributes)
{
(item as AdditionalOp).after();
}
}
}
}

  

动态代理

动态代理,就是在运行时动态地产生代理类,进而产生代理对象。

项目中导入Castle.Core程序包

5种动态代理的生成模式

public interface ICoding
{
void DoSth();
}
public class Coding : ICoding
{
public virtual void DoSth()
{
Console.WriteLine("敲代码咯!");
}
}
public class Architecture : Coding
{
public override void DoSth()
{
Console.WriteLine("架构设计!");
base.DoSth();
}
}
public class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
before();
if (invocation.InvocationTarget != null)
{
invocation.Proceed();
}
after();
} private void before()
{
Console.WriteLine("需求分析!");
}
private void after()
{
Console.WriteLine("测试!");
}
} public class MyInterceptor2 : IInterceptor
{
public void Intercept(IInvocation invocation)
{
before();
if (invocation.InvocationTarget != null)
{
invocation.Proceed();
}
after();
} private void before()
{
Console.WriteLine("需求分析2!");
}
private void after()
{
Console.WriteLine("测试2!");
}
}
public interface IAddItem
{
void AddItem();
} public class AnotherItem : IAddItem
{
public void AddItem()
{
Console.WriteLine("另一个项目开始了!");
}
}

  

class Program
{
static void Main(string[] args)
{
ClassProxy();
ClassProxyWithTarget();
InterfaceProxyWithoutTarget();
InterfaceProxyWithTarget();
InterfaceProxyWithTargetInterface();
Mixin();
Mixin2();
Mixin3();
Mixin4();
Mixin5();
Console.ReadKey();
} static void ClassProxy()
{
Console.WriteLine("\n*************ClassProxy*************\n");
var generator = new ProxyGenerator();
var ProjectDevelopment = generator.CreateClassProxy<Coding>(
new MyInterceptor(),
new MyInterceptor2()
);
ProjectDevelopment.DoSth(); Print(ProjectDevelopment);
} static void ClassProxyWithTarget()
{
Console.WriteLine("\n*************ClassProxyWithTarget*************\n");
var generator = new ProxyGenerator();
var ProjectDevelopment = generator.CreateClassProxyWithTarget<Coding>(
new Architecture(),
new MyInterceptor(),
new MyInterceptor2()
);
ProjectDevelopment.DoSth(); Print(ProjectDevelopment);
} static void InterfaceProxyWithoutTarget()
{
Console.WriteLine("\n*************InterfaceProxyWithoutTarget*************\n");
var generator = new ProxyGenerator();
var ProjectDevelopment = generator.CreateInterfaceProxyWithoutTarget<ICoding>(
new MyInterceptor(),
new MyInterceptor2()
);
ProjectDevelopment.DoSth(); Print(ProjectDevelopment);
} static void InterfaceProxyWithTarget()
{
Console.WriteLine("\n*************InterfaceProxyWithTarget*************\n");
var generator = new ProxyGenerator();
var ProjectDevelopment = generator.CreateInterfaceProxyWithTarget<ICoding>(
new Architecture(),
new MyInterceptor(),
new MyInterceptor2()
);
ProjectDevelopment.DoSth(); Print(ProjectDevelopment);
} static void InterfaceProxyWithTargetInterface()
{
Console.WriteLine("\n*************InterfaceProxyWithTargetInterface*************\n");
var generator = new ProxyGenerator();
var ProjectDevelopment = generator.CreateInterfaceProxyWithTargetInterface<ICoding>(
new Architecture(),
new MyInterceptor(),
new MyInterceptor2()
);
ProjectDevelopment.DoSth(); Print(ProjectDevelopment);
}
static void Print(object o)
{
Console.WriteLine();
Console.WriteLine("GetType():".PadRight(30) + o.GetType());
Console.WriteLine("GetType().BaseType:".PadRight(30) + o.GetType().BaseType); var compositeField = o.GetType().GetField("__target");
Console.WriteLine("__target:".PadRight(30) + compositeField?.FieldType + ", " + compositeField?.Name); foreach (var interfaceType in o.GetType().GetInterfaces())
{
Console.WriteLine("GetType().GetInterfaces():".PadRight(30) + interfaceType);
} foreach (var a in (o as IProxyTargetAccessor).GetInterceptors())
{
Console.WriteLine("GetInterceptors():".PadRight(30) + a);
}
} static void Print2(object o)
{
Console.WriteLine();
Console.WriteLine("GetType():".PadRight(30) + o.GetType());
Console.WriteLine("GetType().BaseType:".PadRight(30) + o.GetType().BaseType); var compositeField = o.GetType().GetField("__target");
Console.WriteLine("__target:".PadRight(30) + compositeField?.FieldType + ", " + compositeField?.Name); foreach (var field in o.GetType().GetFields())
{
if (field.Name.StartsWith("__mixin"))
{
Console.WriteLine("GetType().GetFields():".PadRight(30) + field?.FieldType + ", " + field?.Name);
}
} foreach (var interfaceType in o.GetType().GetInterfaces())
{
Console.WriteLine("GetType().GetInterfaces():".PadRight(30) + interfaceType);
} foreach (var a in (o as IProxyTargetAccessor).GetInterceptors())
{
Console.WriteLine("GetInterceptors():".PadRight(30) + a);
}
} static void Mixin()
{
Console.WriteLine("\n*************CreateClassProxy Mixin*************\n");
var generator = new ProxyGenerator();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new AnotherItem()); var ProjectDevelopment = generator.CreateClassProxy<Coding>(
options,
new MyInterceptor(),
new MyInterceptor2()
);
ProjectDevelopment.DoSth();
Console.WriteLine("\n");
(ProjectDevelopment as IAddItem).AddItem(); Print2(ProjectDevelopment);
} static void Mixin2()
{
Console.WriteLine("\n*************CreateClassProxyWithTarget Mixin*************\n");
var generator = new ProxyGenerator();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new AnotherItem()); var ProjectDevelopment = generator.CreateClassProxyWithTarget<Coding>(
new Architecture(),
options,
new MyInterceptor(),
new MyInterceptor2()
); ProjectDevelopment.DoSth();
Console.WriteLine("\n");
(ProjectDevelopment as IAddItem).AddItem(); Print2(ProjectDevelopment);
} static void Mixin3()
{
Console.WriteLine("\n*************CreateInterfaceProxyWithoutTarget Mixin*************\n");
var generator = new ProxyGenerator();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new AnotherItem()); var ProjectDevelopment = generator.CreateInterfaceProxyWithoutTarget<ICoding>(
options,
new MyInterceptor(),
new MyInterceptor2()
); ProjectDevelopment.DoSth();
Console.WriteLine("\n");
(ProjectDevelopment as IAddItem).AddItem(); Print2(ProjectDevelopment);
} static void Mixin4()
{
Console.WriteLine("\n*************CreateClassProxyWithTarget Mixin*************\n");
var generator = new ProxyGenerator();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new AnotherItem()); var ProjectDevelopment = generator.CreateClassProxyWithTarget<Coding>(
new Architecture(),
options,
new MyInterceptor(),
new MyInterceptor2()
); ProjectDevelopment.DoSth();
Console.WriteLine("\n");
(ProjectDevelopment as IAddItem).AddItem(); Print2(ProjectDevelopment);
} static void Mixin5()
{
Console.WriteLine("\n*************CreateInterfaceProxyWithTargetInterface Mixin*************\n");
var generator = new ProxyGenerator();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new AnotherItem()); var ProjectDevelopment = generator.CreateInterfaceProxyWithTargetInterface<ICoding>(
new Architecture(),
options,
new MyInterceptor(),
new MyInterceptor2()
); ProjectDevelopment.DoSth();
Console.WriteLine("\n");
(ProjectDevelopment as IAddItem).AddItem(); Print2(ProjectDevelopment);
}
}

  

Castle.DynamicProxy的使用的更多相关文章

  1. 基于Autofac, Castle.DynamicProxy的动态WCF解决方案(原创)

    本方案解决了下面3个主要的问题: 1.减少配置,为了避免每次新增service都需要去修改配置文件,包括服务器端跟各个客户端的. 2.能够使用函数重载,泛型函数,以及泛型类. 3.使项目能够快速地在w ...

  2. Castle DynamicProxy

    Introduction¶ Castle DynamicProxy is a library for generating lightweight .NET proxies on the fly at ...

  3. Castle.DynamicProxy Part 1: ClassProxy

    1.Castle中代理对象的分类 总的来说,代理对象大概可以分为2大类: 1.继承类型的代理对象 一类是继承类型的代理类.即:有一个类A,它的代理类是B.B是继承自A的.调用代理类B中的方法时,可以通 ...

  4. castle.dynamicProxy学习笔记

    目的: 可以将castle.dynamicProxy当成代码生成器,快速的生成自己想的代码.这个库经历了这么多年的测试,应该可以用了:D 概念: IInterceptor:拦截器 当方法(属性的本质是 ...

  5. Castle DynamicProxy基本用法(AOP)

    本文介绍AOP编程的基本概念.Castle DynamicProxy(DP)的基本用法,使用第三方扩展实现对异步(async)的支持,结合Autofac演示如何实现AOP编程. AOP 百科中关于AO ...

  6. 使用Castle DynamicProxy (AOP)

    在本文中,我将引导您了解.NET环境中的面向方面编程(AOP)概念,以及如何使用Castle DynamicProxy创建和附加方面.在我们开始之前,让我快速介绍AOP和  IoC.如果您已经熟悉这些 ...

  7. 在 CAP 中使用 AOP ( Castle.DynamicProxy )

    简介 本篇文章主要介绍如何在 CAP 中集成使用 Castle.DynamicProxy,Castle DynamicProxy 是一个用于在运行时动态生成轻量级.NET代理的库.代理对象允许在不修改 ...

  8. AOP之Castle DynamicProxy 动态代理

    这里主要介绍使用castle这个动态代理,在.net一些开源的框架里可以找到它的影子,就连微软的rchard也是使用这个进行方法拦截等可以基于这个进行方法拦截,在这个方面PostSharp算是比较好用 ...

  9. Castle DynamicProxy creation出现COMException(0x800703fa)错误的解决方案

    昨天有客户反馈周末重启服务器后,几台服务器上的应用运行全部出错.大致错误内容如下: COMException(0x800703fa):试图在标记为删除的注册表项上进行不合法的操作. 通过查看异常堆栈, ...

  10. 几种Aop实现及Castle.DynamicProxy的使用

    AoP(Aspect Oriented Programming,面向切面编程) .Net平台AOP技术研究 简单实现 通过继承实现 public interface ICoding { void Do ...

随机推荐

  1. idea中解决spring配置文件命名空间(namespace)出现红色问题

    在配置dubbo项目时,开始时经常出现dubbo错误,如下图: 修改方式已经出现在提示中了,具体这个更改: 1. 解压dubbo-2.5.4.jar这样的包(具体看你使用的版本),选择:META-IN ...

  2. 【IMOOC学习笔记】多种多样的App主界面Tab实现方法(二)

    Fragment实现Tab 首先把activity_main.xml 文件中的ViewPager标签改成Fragment标签 <FrameLayout android:id="@+id ...

  3. Linux中的Makefile

    在Linux中Makefile扮演一个非常重要的角色,我们可以以Linux为平台在上面编写我们需要的C程序代码, 对于C语言来说,Linux是一个非常好的平台来学习.使用.调试.验证C代码的平台,其强 ...

  4. HTML与CSS入门经典(第9版)试读 附随书源码 pdf扫描版​

    HTML与CSS入门经典(第9版)是经典畅销图书<HTML与CSS入门经典>的最新版本,与过去的版本相同,本书采用直观.循序渐进的方法,为读者讲解使用HTML5与CSS3设计.创建并维护世 ...

  5. c#静态方法和非静态方法区别

    c#静态方法和非静态方法区别 C#的类中可以包含两种方法:C#静态方法与非静态方法.那么他们的定义有什么不同呢?他们在使用上会有什么不同呢?让我们来看看最直观的差别:使用了static 修饰符的方法为 ...

  6. 从底层了解ASP.NET体系结构

    导读:  前言  关于ASP.NET的底层的工作机制,最近园子里讨论的甚是火热.相信很多人都看过Rick Strahl先生的一篇经典之作:A low-level Look at the ASP.NET ...

  7. 读取txt里面的数据进行计算

    双在论坛上找到一个问题,有关读取txt里面的数据进行计算的问题. 尝试解决这个问题,获取每一行的X和Y的浮点数据即可.读取文本文件每一行,判断是否为空行,是否符以分隔符号(,)分隔的两个数值.每个数值 ...

  8. day06.1-磁盘管理

    1. 添加磁盘 打开虚拟机,依次点击"编辑虚拟机设置" |—> "添加" |—> "硬盘" |—> "选择硬盘类 ...

  9. requests模块demo

    import urllib.request import requests from requests.auth import HTTPBasicAuth from requests.auth imp ...

  10. 洛谷P3724 [AH2017/HNOI2017]大佬(决策单调性)

    传送门 这个思路很妙诶->这里 以下为了方便,我把自信说成血量好了 虽然表面上看起来每一天有很多种选择,然而我们首先要保证的是不死,然后考虑不死的情况下最多能拿出多少天来进行其他操作.不死可以d ...