Castle DynamicProxy
Posted chucklu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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?
以上是关于Castle DynamicProxy的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Castle.DynamicProxy 中的 ProxyGenerationHook 访问自定义方法属性
如何在 Castle.DynamicProxy 中使用 IInterceptor?
在 C# 中使用 Castle.DynamicProxy 对数组进行排序