Castle DynamicProxy
https://github.com/castleproject/Core/blob/master/docs/dynamicproxy.md
DynamicProxy generates proxies for your objects that you can use to transparently add or alter behavior to them, provide pre/post processing and many other things. Following are just a few better known and popular usages of DynamicProxy:
- Castle Windsor uses proxies to enable its interception capabilities and for typed factories
- Moq uses it to provide "the most popular and friendly mocking framework for .NET"
- NSubstitute uses it to provide "a friendly substitute for .NET mocking frameworks"
- FakeItEasy uses it to provide "the easy mocking library for .NET"
- Rhino Mocks uses it to provide "a dynamic mock object framework for the .NET platform"
- NHibernate uses it to provide lazy loading capabilities (pre-v4.0)
- Entity Framework Core uses it in its package Microsoft.EntityFrameworkCore.Proxies to provide lazy-loading proxies
If you're new to DynamicProxy you can read a quick introduction, browse through description of the core types in the library, or go into more advanced, in detail discussion:
- Kinds of proxy objects
- Leaking this
- Make proxy generation hooks purely functional
- Overriding Equals/GetHashCode on proxy generation hook
- Make your supporting classes serializable
- Use proxy generation hooks and interceptor selectors for fine grained control
- SRP applies to interceptors
- Behavior of by-reference parameters during interception
- Optional parameter value limitations
- Asynchronous interception
ℹ️Where is Castle.DynamicProxy.dll
?: DynamicProxy used to live in its own assembly. As part of changes in version 2.5 it was merged into Castle.Core.dll
and that's where you'll find it.
⚠️Use of a single ProxyGenerator's instance: If you have a long running process (web site, windows service, etc.) and you have to create many dynamic proxies, you should make sure to reuse the same ProxyGenerator instance. If not, be aware that you will then bypass the caching mechanism. Side effects are high CPU usage and constant increase in memory consumption.
Kinds of Proxy Objects
Out of the box, the DynamicProxy provides several kinds of proxy objects that you can use. They fall into two broad categories:
Inheritance-based
Inheritance-based proxies are created by inheriting a proxy class. The proxy intercepts calls to virtual members of the class and forwards them to base implementation. In this case, the proxy and proxied object are one. This also means you can't create inheritance based proxy for a pre-existing object. There's one inheritance-based proxy kind in DynamicProxy.
- Class proxy - creates an inheritance-based proxy for a class. Only virtual members of the class can be intercepted.
Composition-based
Composition-based proxy is a new object that inherits from proxied class/implements proxied interface and (optionally) forwards intercepted calls to the target object. DynamicProxy exposes the following composition-based proxy types:
-
Class proxy with target - this proxy kind targets classes. It is not a perfect proxy and if the class has non-virtual methods or public fields these can't be intercepted giving users of the proxy inconsistent view of the state of the object. Because of that, it should be used with caution.
-
Interface proxy without target - this proxy kind targets interfaces. It does not expect target object to be supplied. Instead, interceptors are expected to provide implementation for all members of the proxy.
-
Interface proxy with target - as its name implies wraps objects implementing selected interfaces forwarding calls to these interfaces to the target object.
-
Interface proxy with target interface - this kind of proxy is kind of a hybrid of two other interface proxy kinds. It allows, but not requires target object to be supplied. It also allows the target to be swapped during the lifetime of the proxy. It is not tied to one type of the proxy target so one proxy type can be reused for different target types as long as they implement the target interface.
External resources
- Tutorial on DynamicProxy discussing with examples all kinds of proxies
- Castle Dynamic Proxy not intercepting method calls when invoked from within the class
- Why won't DynamicProxy's interceptor get called for each virtual method call?
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-05-20 Create an ASP.NET Core web app in Visual Studio Code
2017-05-20 CSS元素选择器 element selector(type selector)
2017-05-20 如何查看本机正在监听的端口
2017-05-20 Error 0x80070020 when you try to start a Web site in IIS 7.0