在.NET中动态拦截调用 [英] Dynamic interception of calls in .NET

查看:128
本文介绍了在.NET中动态拦截调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道.NET是否支持任何形式的运行时方法调用(或属性调用)的动态拦截。也就是说,你可以拦截一个没有静态编译信息的对象的调用,如接口(沿着CORBA DII的一行(链接文本)或COM的IDispatch)。



如果没有,C#4.0中的新的动态类型对象功能将在此帮助

解决方案

没有任何内置的东西可以拦截你不能控制实例化的对象。同样地,在即将到来的.net 4.0中也没有新的设施。



如果您可以控制实例化:


  1. 如果您的对象可以是MarshalByRef可以使用 RealProxy

  2. 您可以使用不少IOC容器,例如 LinFu 城堡动态代理

  3. 您可以使用像 PostSharp Mono Cecil 微软CCI 将作为后期编译步骤的截取重写程序集。

如果您无法控制实例化


  1. 您可以使用 ICorDebug - .Net调试很难使用和沉重的API。

  2. 您可以使用 ICorProfiler - .Net分析API,使用起来也很复杂。另外,您可以看一下动态语言,如IronRuby,它具有内置的 alias_method define_method goodness(允许您重新定义任何东西),所以拦截在。






    .NET 4.5引入了一种方法来 ReJIT 方法,为方法拦截提供了另一个钩子(前提是您没有运行并发/ bg GC,方法没有内联)


    I am curious to learn if .NET supports any form of dynamic interception of method calls (or properties invocations) at runtime. That is, can you intercept a call to an object without static compilation information such as an interface (along the lines of the CORBA DII (link text) or COM's IDispatch).

    If not, would the new 'Dynamically Typed Objects' feature in C# 4.0 help in this regard.

    解决方案

    There is nothing built-in that allows you to intercept an object that you can not control instantiation of. Similarly, there will be no new facilities for this in the upcoming .net 4.0.

    If you can control instantiation:

    1. If your object can be MarshalByRef you can use RealProxy.
    2. You could use quite a few IOC containers, eg. LinFu, Castle Dynamic Proxy
    3. You could use a tool like PostSharp, Mono Cecil or Microsoft CCI to rewrite your assemblies with the interceptions as a post compile step.

    If you can not control instantiation

    1. You can use ICorDebug - the .Net debugging APIs which are really hard to use and heavy.
    2. You can use ICorProfiler - the .Net profiling APIs where are also pretty complicated to use.

    Alternatively, you could look at a dynamic language like IronRuby, which has a built-in alias_method and define_method goodness (which allows you to redefine anything), so interception baked in.


    .NET 4.5 is introducing a method to ReJIT methods which provides another hook for method interception (provided you are not running a concurrent/bg GC and the method is not inlined)

    这篇关于在.NET中动态拦截调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆