The agent object is dynamically dressed by the following code.
switch (handlerField.Name) {case mixinHandlerFieldName: {ilGenerator.Emit (OpCodes.Callvirt, typeof (IMixinInvocationHandler) .GetMethod ( "Invoke")); break;} default: {ilGenerator.Emit (OpCodes.Callvirt, typeof (IInvocationHandler) .GetMethod ("Invoke"); Break;}}
The ImixinInvocationHandler and IinvocationHandler interface are incorporated in the construction of the agent object, as follows:
// Class AspectSharpenginePublic static object wrap (object target) {type origintype = target.gettype (); string typeName = Origintype.Fullname;
IinvocationHandler Handler = GetInvocationHandler (Target); iMixinInvocationHandler MixinHandler = getMixinInvocationHandler (); iMixin [] Mixins = getMixins (Typename);
// ...
return ProxyGenerator.CreateInstance (_typeCache [typeName], mixins, handler, mixinHandler);.} proxy object passing in the constructor of the object as well as a mixer where the interface is implemented as a IInvocationHandler DefaultInvocationHandler, to achieve IMixinvocationHandler MixinInvocationHandler, GetMixins from Get all the mixers on the objects in the configuration file.
The process of intercept:
1. The proxy object calls DefaultInvocationHandler.Invoke;. 2 Invoke method is called to obtain methods GetMethodInvocation configuration file interceptor; IMethodInvocation GetMethodInvocation (MethodInfo method, object [] arguments) {string typeName = _target.GetType () FullName.;
IASPECT Aspect = getASpectFortype (Typename);
if (aspect = null && aspect.PointCuts = null!!) {IMatch match = _methodMatcher.Match (method, aspect.PointCuts); return new InterceptorMethodInvocation (_target, method, arguments, match.Interceptors); // match.Interceptors of All load points of the method.} Else {return new defaultMethodInvocation (_target, method, arguments);}}
At this time there are two cases
The A method does not define an intercept point. 3. Create a defaultMethodInvocation; 4. Call the invokeMethod to perform the real method implementation code. B. Method has a defined intercept point. 3. Create an interceptorMethodInvocation . control); 4 proceed method invocation repeater; // class IntreceptorMethodInvocation public override object Proceed () {if (_currentInterceptorIndex == (_interceptors.Length - 1)) {return InvokeMethod (_target, _method, _arguments);}
_CurrentInterceptorIndex ;
Iinterceptor currentinterceptor = _interceptors [_CurrentInterceptorIndex];
if (currentInterceptor is IMethodInterceptor) {return ((IMethodInterceptor) currentInterceptor) .Invoke (this);} else {return InvokeMethod (_target, _method, _arguments);}} // Invoke method call interceptor, the method then performs true Code. // The interceptor must implement the IMETHODITERCEPTOR interface. This code can only call the _interceptors [0] interceptor code, how other interceptor code is implemented? Answer in the interceptor we implemented, typically in the interceptor, typical The invoke code is as follows: public object invocation {try {// ... returncture.proceed (); this is a must, which will continue the interceptor process.} Catch (Exception E) {//. ..} return null;} mixer mixing process
1. Call the MixinInvocationHandler.invoke method; 2. Get the method of acquiring the mixer through the GetTargetMethod of DefaultInvocationHandler. 3. Implementation code for the method.