Castle.DynamicProxy的使用
简单实现
通过继承实现
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的使用的更多相关文章
- 基于Autofac, Castle.DynamicProxy的动态WCF解决方案(原创)
本方案解决了下面3个主要的问题: 1.减少配置,为了避免每次新增service都需要去修改配置文件,包括服务器端跟各个客户端的. 2.能够使用函数重载,泛型函数,以及泛型类. 3.使项目能够快速地在w ...
- Castle DynamicProxy
Introduction¶ Castle DynamicProxy is a library for generating lightweight .NET proxies on the fly at ...
- Castle.DynamicProxy Part 1: ClassProxy
1.Castle中代理对象的分类 总的来说,代理对象大概可以分为2大类: 1.继承类型的代理对象 一类是继承类型的代理类.即:有一个类A,它的代理类是B.B是继承自A的.调用代理类B中的方法时,可以通 ...
- castle.dynamicProxy学习笔记
目的: 可以将castle.dynamicProxy当成代码生成器,快速的生成自己想的代码.这个库经历了这么多年的测试,应该可以用了:D 概念: IInterceptor:拦截器 当方法(属性的本质是 ...
- Castle DynamicProxy基本用法(AOP)
本文介绍AOP编程的基本概念.Castle DynamicProxy(DP)的基本用法,使用第三方扩展实现对异步(async)的支持,结合Autofac演示如何实现AOP编程. AOP 百科中关于AO ...
- 使用Castle DynamicProxy (AOP)
在本文中,我将引导您了解.NET环境中的面向方面编程(AOP)概念,以及如何使用Castle DynamicProxy创建和附加方面.在我们开始之前,让我快速介绍AOP和 IoC.如果您已经熟悉这些 ...
- 在 CAP 中使用 AOP ( Castle.DynamicProxy )
简介 本篇文章主要介绍如何在 CAP 中集成使用 Castle.DynamicProxy,Castle DynamicProxy 是一个用于在运行时动态生成轻量级.NET代理的库.代理对象允许在不修改 ...
- AOP之Castle DynamicProxy 动态代理
这里主要介绍使用castle这个动态代理,在.net一些开源的框架里可以找到它的影子,就连微软的rchard也是使用这个进行方法拦截等可以基于这个进行方法拦截,在这个方面PostSharp算是比较好用 ...
- Castle DynamicProxy creation出现COMException(0x800703fa)错误的解决方案
昨天有客户反馈周末重启服务器后,几台服务器上的应用运行全部出错.大致错误内容如下: COMException(0x800703fa):试图在标记为删除的注册表项上进行不合法的操作. 通过查看异常堆栈, ...
- 几种Aop实现及Castle.DynamicProxy的使用
AoP(Aspect Oriented Programming,面向切面编程) .Net平台AOP技术研究 简单实现 通过继承实现 public interface ICoding { void Do ...
随机推荐
- 依赖注入(DI)与控制反转(IOC)
DI(依赖注入,Dependency Injection),和所谓的IoC(控制反转,Inversion of Control )是一个意思. DI是一种通过接口实现松耦合的设计模式.初学者可能会好奇 ...
- 文章推荐一个Java程序员跟大家谈谈从业心得
一个Java程序员跟大家谈谈从业心得 2017-10-21 java那些事 java那些事 java那些事 微信号 csh624366188 功能介绍 分享java开发中常用的技术,分享软件开发中各种 ...
- 服务器部署php项目
windows服务器 首先打开开始菜单,点击运行. 然后输入mstsc,确定 输入你的服务器IP,点击连接 这里选择 是 然后就到了登录界面,输入用户名和密码就可以了 linux服 ...
- docker与虚拟机性能比较(转)
http://blog.csdn.net/cbl709/article/details/43955687 本博客来源于我的个人博客: www.chenbiaolong.com 欢迎访问. 概要 doc ...
- 6.House Robber(简单版抢银行)
Level: Easy 题目描述: You are a professional robber planning to rob houses along a street. Each house ...
- Git 的简单测试
Git 简介 Git(读音为/gɪt/.)是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开 ...
- Subversion Server Edge的安装使用
1.Subversion Server Edge的搭建 当在操作系统为64位的配置服务器上部署时只能够选择Collabnet Subversion Edge,它集合了Subversion所需要一切资源 ...
- Educational Codeforces Round 13 B
Description The girl Taylor has a beautiful calendar for the year y. In the calendar all days are gi ...
- 华东交通大学2015年ACM“双基”程序设计竞赛1004
Problem D Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Sub ...
- day34 协程
1. 前提 之前我们学习了线程.进程的概念,了解了在操作系统中进程是资源分配的最小单位,线程是CPU调度的最小单位.按道理来说我们已经算是把cpu的利用率提高很多了.但是我们知道无论是创建多进程还 ...