Jump to content
Tuts 4 You

[.NET TIP] Easy dynamic IL generation


sirp

Recommended Posts

Posted (edited)

DynamicProxy


...

...

methodIlGenerator.Emit(OpCodes.Ldc_I4_2);

methodIlGenerator.Emit(OpCodes.Ldarg_1);

methodIlGenerator.Emit(OpCodes.Ceq);

methodIlGenerator.Emit(OpCodes.Ldc_I4_0);

methodIlGenerator.Emit(OpCodes.Ceq);

methodIlGenerator.Emit(OpCodes.Stloc_3);

methodIlGenerator.Emit(OpCodes.Ldloc_3);

...

...
...if that is too much trouble fumbling with the low-level opcode can also spread an alternative to the comfortable
castleproject DynamicProxy and use the local IL-generation mechanisms (easy type / easy method etc.) so that's really "Easy"
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. Both classes and interfaces can be proxied, however only virtual members can be intercepted.
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.
You can use DynamicProxy to generate lightweight proxies on the fly for one or more interfaces or even concrete classes (but only virtual methods will be intercepted).
Why use proxies?
Proxy objects can assist in building a flexible application architecture because it allows functionality to be transparently added to code without modifying it. For example, a class could be proxied to add logging or security checking without making the code aware this functionality has been added.
For example, NHibernate, an object/relational mapper uses DynamicProxy to provide lazy loading of data without the domain model classes being aware of this functionality.
Creating Proxies
DynamicProxy can generate lightweight proxies on the fly for one or more interfaces as well as concrete classes, however only virtual methods will be intercepted. There are 4 different create proxy methods available on the ProxyGenerator class:
* CreateInterfaceProxyWithTarget creates a proxy object intercepting calls to the members of the interface on the target object.
* CreateInterfaceProxyWithTargetInterface creates a proxy object intercepting calls to the members of the interface on the target object. Interceptors can provide an alternative implementation target swapped at runtime.
* CreateInterfaceProxyWithoutTarget creates a proxy object intercepting calls to members of the interface on the target object generated at runtime by an interceptor.
* CreateClassProxy creates a proxy object intercepting calls to virtual members of a class. Abstract classes can also be proxied.
Who is using DynamicProxy
The following open source projects use DynamicProxy which may assist you with using DynamicProxy:
* Castle Project
* NHibernate
* NHibernate ProxyGenerators
* Rhino Mocks
* Moq
* NMock2
* Ninject
* re-motion mixins
* EasyProp

url

and some nice big nose ;) tutorials

url

Edited by sirp

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...