Introduction

Castle DynamicProxy is a library for generating lightweight .NET proxies on the fly at runtime. Proxy objects allow calls to members of an object to be intercepted without modifying the code of the class.

DynamicProxy differs from the proxy implementation built into the CLR which requires the proxied class to extend MarshalByRefObject. Extending MashalByRefObject to proxy an object can be too intrusive because it does not allow the class to extend another class and it does not allow transparent proxying of classes. Additionally Castle DynamicProxy provides capabilities beyond what standard CLR proxies can do, for example it lets you mix in multiple objects.

Requirements

To use Castle DynamicProxy you need the following environment:

  • one of the following runtimes installed
    • .NET version 3.5 sp1 or newer
    • Silverlight version 4 or newer
  • Castle.Core.dll (assembly where DynamicProxy lives)

At this time Mono is not supported

 

 

DynamicProxy assembly

In previous versions (up to v2.2) DynamicProxy used to live in its own assembly Castle.DynamicProxy.dll. It was later moved to Castle.Core.dll and now no other assembly is required to use it.

 

Interception pipeline

Another way, and it's how DP is used mostly, is by adding behavior to the proxied objects. That's what the IInterceptor interface you'll find in DynamicProxy is for. You use interceptors to inject behavior into the proxy.

The picture above shows schematically how that works.

  • The blue rectangle is the proxy. Someone calls a method on the proxy (denoted by yellow arrow). Before the method reaches the target object it goes through a pipeline of interceptors.
  • Each interceptor gets a IInvocation object (which is another important interface from Dynamic Proxy), that holds all the information about current request, like the MethodInfo of the method called, along with its parameters and returned value, reference to the proxy, as well as proxied object, and few others. Each interceptor gets its chance to inspect and change those values before the actual method on the target object is called.
    So for example at this stage you can log debug information about what parameters were passed to the method, or validate them. Then, the interceptor has to call invocation.Proceed(), to pass control further down the pipeline. An interceptor can call Proceed at most once, otherwise an exception is thrown.
  • After last interceptor calls Proceed, the actual method on proxied object is invoked, and then the call travels back, up the pipeline (green arrow) giving each interceptor chance to inspect and act on, returned value, or thrown exceptions.
  • Finally the proxy returns the value held by invocation.ReturnValue as the return value of called method.

 

Interceptor example

If this was not clear enough, here's a sample interceptor, that shows how it works:

[Serializable]

public class Interceptor : IInterceptor

{

public void Intercept(IInvocation invocation)

{

Console.WriteLine("Before target call");

try

{

invocation.Proceed();

}

catch(Exception)

{

Console.WriteLine("Target threw an exception!");

throw;

}

finally

{

Console.WriteLine("After target call");

}

}

}

Hopefully, at this stage you have a pretty good idea about what DynamicProxy is, how it works, and what it's good for. In the next chapter we'll dive into some more advanced capabilities, plugging into, and influencing the process of generating proxy class.

See also

Kinds of proxy objects

Castle DynamicProxy的更多相关文章

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

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

  2. Castle.DynamicProxy Part 1: ClassProxy

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

  3. castle.dynamicProxy学习笔记

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

  4. Castle DynamicProxy基本用法(AOP)

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

  5. 使用Castle DynamicProxy (AOP)

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

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

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

  7. AOP之Castle DynamicProxy 动态代理

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

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

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

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

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

随机推荐

  1. Testing - Tips

    1 --- 冒烟测试.可用性测试和回归测试的区别? 在测试领域中,冒烟测试(smoke test).可用性测试(sanity test)和回归测试(regression test)彼此之间很相似,范围 ...

  2. 创建数据库和表相关的SQL语句

    SQL server注释语句有两种: 一种是单行注释,一种是多行注释. ******************** 单行注释用:--注释一行内容 多行注释用:/* *注释 *多行内容 */ 创建数据库: ...

  3. MVC中Action的执行过程

    接着上一篇:MVC控制器的激活过程 一.代码现行,该伪代码大致解析了Action的执行的过程 try { Run each IAuthorizationFilter's OnAuthorization ...

  4. [爬虫资源]各大爬虫资源大汇总,做我们自己的awesome系列

      大数据的流行一定程序导致的爬虫的流行,有些企业和公司本身不生产数据,那就只能从网上爬取数据,笔者关注相关的内容有一定的时间,也写过很多关于爬虫的系列,现在收集好的框架希望能为对爬虫有兴趣的人,或者 ...

  5. Python语言特性之1:函数参数传递

    问题:在Python文档中好像没有明确指出一个函数参数传递的是值传递还是引用传递.如下面的代码中"原始值"是不放生变化的: class PassByReference: def _ ...

  6. 类UNIX操作系统概念

    摘要:对unix os上的一些基本概念做一个统一的梳理,以下内容转自互联网和相关书籍 一 进程组.会话.控制终端 进程组---------------------------------------- ...

  7. 背水一战 Windows 10 (7) - 控件 UI: VisualState, VisualStateManager, 控件的默认 UI

    [源码下载] 背水一战 Windows 10 (7) - 控件 UI: VisualState, VisualStateManager, 控件的默认 UI 作者:webabcd 介绍背水一战 Wind ...

  8. Flume

    http://www.ibm.com/developerworks/cn/data/library/bd-1404flumerevolution/index.html https://flume.ap ...

  9. JavaScript深究系列 [一]

    1. JavaScript中 = = = 首先,== equality 等同,=== identity 恒等. ==, 两边值类型不同的时候,要先进行类型转换,再比较. ===,不做类型转换,类型不同 ...

  10. Scalaz(3)- 基础篇:函数概括化-Generalizing Functions

    Scalaz是个通用的函数式编程组件库.它提供的类型.函数组件都必须具有高度的概括性才能同时支持不同数据类型的操作.可以说,scalaz提供了一整套所有编程人员都需要的具有高度概括性的通用函数,它是通 ...