One of the main application scenarios of dynamic proxy is to implement AOP-intercept method calls and add your own preprocessing, post-processing, or around und processing.
I implemented support for these interceptions in esbasic. emit.
First, we will introduce two interceptors: /// <Summary>
/// Imethodinterceptor intercepts the method and adds it to preprocessing and post-processing.
/// </Summary>
Public Interface Imethodinterceptor
{
Void Preprocess ( Interceptedmethod Method );
VoidPostprocess (InterceptedmethodMethod,ObjectReturnval );
}
Imethodinterceptor is easy to understand and used to intercept insertion preprocessing and post-processing. This is easy to understand. /// <Summary>
/// Iaroundinterceptor intercepts methods. Note: The call to the target method must be triggered.
/// </Summary>
Public Interface Iaroundinterceptor
{
Object Aroundcall (InterceptedmethodMethod );
}
Iaroundinterceptor completes the handling of the exception. What is round processing? For example, if we want to catch exceptions of the target method, we need to use trycatch to enclose the target method. This is an example of how to handle the exception.
The preceding interface method has an interceptedmethod parameter, which encapsulates the basic information of the intercepted target method. Public Sealed Class Interceptedmethod
{
# Region Ctor
Public Interceptedmethod (){}
Public Interceptedmethod ( Object _ Target, methodinfo _ method, Object [] Paras)
{
This . Target = _ Target;
This . Method = _ Method;
This . Arguments = Paras;
}
# Endregion
# Region Method
Private Methodinfo method;
/// <Summary>
/// Method intercepted target method
/// </Summary>
Public MethodinfoMethod
{
Get { Return Method ;}
Set {Method = Value ;}
}
# Endregion
# Region Target
Private Object Target;
/// <Summary>
/// The object on which the target method is called.
/// </Summary>
Public Object Target
{
Get { Return Target ;}
Set {Target = Value ;}
}
# Endregion
# Region Arguments
Private Object [] Arguments;
/// <Summary>
/// Arguments calls parameters of intercepted Methods
/// </Summary>
Public Object [] Arguments
{
Get { Return Arguments ;}
Set {Arguments = Value ;}
}
# Endregion
# Region Invoke
/// <Summary>
/// Invoke execution target method
/// </Summary>
Public Object Invoke ()
{
Return This . Method. Invoke ( This . Target, This . Arguments );
}
# Endregion
}
well, how do I use esbasic. emit to create a dynamic proxy? You only need to call Public static tinterface createaopproxy tinterface > ( Object origin, imethodinterceptor methodinterceptor, iaroundinterceptor aroundinterceptor)
If no interception processing is required, the corresponding parameter can be null.
For example, we need to use a dynamic proxy to intercept the exceptions thrown by all method calls of the icomputer and record the logs. Icomputer is defined as follows:Public Interface Icomputer
{
IntAdd (IntA,IntB );
}
Public Class COMPUTER:Icomputer
{
Public Int Add ( Int A, Int B)
{
Throw New Exception ( " Testexception " );
Return A + B;
}
}
I can intercept exceptions by using the owner, so I implement an iaroundinterceptor: Public Class Exceprionaroundinterceptor:Iaroundinterceptor
{
# Region Iaroundinterceptor Member
Public Object Aroundcall (InterceptedmethodMethod)
{
Try
{
Return Method. Invoke ();
}
Catch (Exception ee)
{
EE = EE;
// . Log exception
Throw ;
}
}
# Endregion
}
Now, we can obtain the reference of the dynamic proxy for the icomputer as follows:IcomputerProxy = Esbasic. emit. application.Dynamicproxyfactory. Createaopproxy < Icomputer > ( New Computer(), Null , New Exceprionaroundinterceptor());
Proxy. Add ( 1 , 2 );
In this way, the exception thrown by the add method will beExceprionaroundinterceptorInterception.
Finally, let me remind you that if you want the computer to not inherit from the icomputer, the running results will be the same. You should have seenDynamicproxyfactory. The first parameter of the createaopproxy method is the object type, rather than the generic T. This indicates that as long as the corresponding object contains the same method as the target interface (the signature is exactly the same) you can -- this is the so-called "face change" capability. You can see more details here.
Because the dynamic proxy is implemented using emit, you can write it with your hand-writtenCodeThere is no difference.
Download esbasic. emit. dll.