- Notifications
You must be signed in to change notification settings - Fork13
NProxy is a library for .NET to create lightweight dynamic proxies
License
mtamme/NProxy
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
NProxy is a library for .NET to create lightweight dynamic proxies.
To install NProxy, run the following command in thePackage Manager Console
PM> Install-Package NProxy.Core
There are already a few libraries out there which deal with dynamic proxy generation so why another dynamic proxy library?The answers which lead to the goals of the NProxy project can be summarized as follows:
- Provide an API to generate dynamic proxies based upon unsealed classes, abstract classes, interfaces and delegates in a unified way.
- General focus on quality attributes like performance, extensibility and lightweightness.
- Treat generic methods not as aliens and massively improve their invocation performance.
- Support the invocation of intercepted base methods.
- Make a library available which can be used as a base for AOP frameworks, mocking libraries, ...
- Dynamic proxy creation must be thread-safe.
Dynamic proxies provide an alternate, dynamic mechanism for implementing many common design patterns,including the Facade, Bridge, Interceptor, Decorator, Proxy, and Adapter patterns. While all of thesepatterns can be easily implemented using ordinary classes instead of dynamic proxies, in many cases thedynamic proxy approach is more convenient and compact and can eliminate a lot of handwritten or generatedclasses.
At the heart of the dynamic proxy mechanism is theIInvocationHandler
interface, shown below.
publicinterfaceIInvocationHandler{objectInvoke(objecttarget,MethodInfomethodInfo,object[]parameters);}
The job of an invocation handler is to actually perform the requested method invocation on behalf of a dynamicproxy. The invocation handler is passed a target object, aMethodInfo
object (from theSystem.Reflection
namespace)and an array of parameters; in the simplest case, it could simply call the methodMethodInfo.Invoke()
and return theresult.MethodInfo.Invoke()
directly invokes the target method without utilizing reflection.
Every proxy has an associated invocation handler that is called whenever one of the proxy's methods is called.Proxies can be created from unsealed classes, abstract classes, interfaces and delegates, and can implementan arbitrary number of interfaces. All interfaces are implemented explicitly to avoid member name conflicts.
There are some known limitation when it comes to event, property or method interception. For class based proxiesonly non-static and only abstract or virtual members can be intercepted.
To exclude events, properties and methods from beeing intercepted just apply theNonInterceptedAttribute
on themember which should not be intercepted. This attribute has no effect when applied to abstract members.You can also implement your ownIInterceptionFilter
if you need full control about which member is going to beintercepted.
publicinterfaceIInterceptionFilter{boolAcceptEvent(EventInfoeventInfo);boolAcceptProperty(PropertyInfopropertyInfo);boolAcceptMethod(MethodInfomethodInfo);}
To create dynamic proxies of only internally visible types, just add the following assembly attribute to your project.
[assembly:InternalsVisibleTo("NProxy.Dynamic")]
If you are using the .NET Framework you have to add the following assembly attribute to your project.
[assembly:InternalsVisibleTo("NProxy.Dynamic, PublicKey=002400000480000094000000060200000024000052534131000400000100010031d0e185f342141fb582a63c5c3706ee107a49b7c4c988587512e9cf2d02473280bd9d5cf129d118978bb753339b1819c5f836a0940a0c3ec153ccad71b4786a388da0b4b9531b405d57ce00ac02ee019001eb1bcfdaa0afa1d1542adec526e1165ce740dd2d31ad682c4c8d9b305bc64c3ebb029dffa773d1f9e0e9a5847885")]
Seehere for details.
Seehere for details.
Seehere for details.
© Martin Tamme. See LICENSE for details.
About
NProxy is a library for .NET to create lightweight dynamic proxies