コード例 #1
0
        private static void MethodIntercept(ILGenerator ilGenerator, MethodInfo methodInfo, IInterceptor[] interceptors, LocalBuilder[] local,
            bool[] boolean, Action<ILGenerator> method)
        {
            var methodInterceptors = interceptors.GetMethodInterceptor();
            if (!methodInterceptors.Any())
            {
                method(ilGenerator);
                return;
            }
            var invocationType = InternalDynamicTypeProvider.CreateType<IMethodInvocation>();
            var returnParameterType = InternalDynamicTypeProvider.CreateType<IReturnParameter>();

            var parameterType = InternalDynamicTypeProvider.CreateType<IParameter>();
            var setParameters = invocationType.GetMethod("set_Parameters");
            var setParameterValue = parameterType.GetMethod("set_Value");
            var getParameterValue = parameterType.GetMethod("get_Value");
            var setParameterName = parameterType.GetMethod("set_Name");
            var setParameterInfo = parameterType.GetMethod("set_ParameterInfo");
            var getParameterName = typeof(ParameterInfo).GetMethod("get_Name");
            var getCurrentParameter = typeof(InterceptorExtends).GetMethod("GetCurrentParameter");

            var setReturnParameter = invocationType.GetMethod("set_ReturnParameter");
            var getReturnValue = returnParameterType.GetMethod("get_Value");
            var setReturnType = returnParameterType.GetMethod("set_ReturnType");
            var setReturnValue = returnParameterType.GetMethod("set_Value");
            var returnParameterLocal = ilGenerator.DeclareLocal(returnParameterType);
            var getReturnType = typeof(MethodInfo).GetMethod("get_ReturnType");
            var setInterceptedMethod = invocationType.GetMethod("set_InterceptedMethod");
            var setInterceptedType = invocationType.GetMethod("set_InterceptedType");
            var setInterceptedInstance = invocationType.GetMethod("set_InterceptedInstance");
            var getExecutedHandled = invocationType.GetMethod("get_ExecutedHandled");
            var getGetMethodInterceptor = typeof(InterceptorExtends).GetMethod("GetMethodInterceptor");
            var getGetExecutedMethodInterceptor = typeof(InterceptorExtends).GetMethod("GetExecutedMethodInterceptor");
            var onMethodExecuting = typeof(IMethodInterceptor).GetMethod("OnMethodExecuting");
            var onMethodExecuted = typeof(IMethodInterceptor).GetMethod("OnMethodExecuted");
            var interceptorLocal = ilGenerator.DeclareLocal(typeof(IMethodInterceptor[]));
            var executedInterceptorLocal = ilGenerator.DeclareLocal(typeof(IMethodInterceptor[]));


            var parametersLocal = ilGenerator.DeclareLocal(typeof(IParameter[]));
         


            var invocationLocal = ilGenerator.DeclareLocal(invocationType);
            var endLable = ilGenerator.DefineLabel();
            ilGenerator.LoadLocal(local[0]).Call(getGetMethodInterceptor).StoreLocal(interceptorLocal);
            ilGenerator.LoadLocal(local[0]).Call(getGetExecutedMethodInterceptor).StoreLocal(executedInterceptorLocal);
            ilGenerator.New(invocationType.GetConstructor(Type.EmptyTypes)).StoreLocal(invocationLocal);
            ilGenerator.LoadLocal(invocationLocal).LoadLocal(local[1]).Callvirt(setInterceptedMethod);
            ilGenerator.LoadLocal(invocationLocal).LoadLocal(local[2]).Callvirt(setInterceptedType);
            ilGenerator.LoadLocal(invocationLocal).This().Callvirt(setInterceptedInstance);
            ilGenerator.New(returnParameterType.GetConstructor(Type.EmptyTypes)).StoreLocal(returnParameterLocal);
            if (boolean[0]) ilGenerator.LoadLocal(returnParameterLocal).LoadLocal(local[1]).Callvirt(getReturnType).Callvirt(setReturnType);
            else ilGenerator.LoadLocal(returnParameterLocal).Typeof(typeof(void)).Callvirt(setReturnType);
            ilGenerator.LoadLocal(invocationLocal).LoadLocal(returnParameterLocal).Callvirt(setReturnParameter);

            ilGenerator.NewArray(typeof(IParameter), methodInfo.GetParameters().Length).StoreLocal(parametersLocal);
            ilGenerator.ForEach(methodInfo.GetParameterTypes(), (il, arg, i) =>
            {
                var argLocal = ilGenerator.DeclareLocal(parameterType);
                ilGenerator.New(parameterType.GetConstructor(Type.EmptyTypes)).StoreLocal(argLocal);
                ilGenerator.LoadLocal(argLocal).LoadArgument(i + 1).Box(arg).Callvirt(setParameterValue);
                ilGenerator.LoadLocal(argLocal).LoadLocal(local[1]).LoadInt(i).Call(getCurrentParameter).Callvirt(setParameterInfo);
                ilGenerator.LoadLocal(argLocal).LoadLocal(local[1]).LoadInt(i).Call(getCurrentParameter).Callvirt(getParameterName).Callvirt(setParameterName);
                ilGenerator.LoadLocal(parametersLocal).LoadInt(i).LoadLocal(argLocal).SetArrayItemRef();
            });
            ilGenerator.LoadLocal(invocationLocal).LoadLocal(parametersLocal).Callvirt(setParameters);

            ilGenerator.ForEach(methodInterceptors, (_, interceptor, index) =>
            _.LoadLocal(interceptorLocal).LoadArrayItem(index).LoadLocal(invocationLocal)
            .Callvirt(onMethodExecuting).LoadLocal(invocationLocal).Callvirt(getExecutedHandled).False(endLable));

            ilGenerator.ForEach(methodInfo.GetParameterTypes(), (il, arg, i) =>
            {
                ilGenerator.LoadLocal(parametersLocal).LoadArrayItem(i).Callvirt(getParameterValue).UnBox(arg).StroeArgument(i + 1);
            });
            method(ilGenerator);


            if (boolean[0]) ilGenerator.LoadLocal(returnParameterLocal).LoadLocal(local[3]).Callvirt(setReturnValue);
            ilGenerator.ForEach(methodInterceptors.OrderBy(i => i.ExecutedOrder), (_, interceptor, index) =>
            _.LoadLocal(executedInterceptorLocal).LoadArrayItem(index).LoadLocal(invocationLocal).
            Callvirt(onMethodExecuted).LoadLocal(invocationLocal).Callvirt(getExecutedHandled).False(endLable));
            ilGenerator.MarkLabelFor(endLable);
            if (boolean[0]) ilGenerator.LoadLocal(returnParameterLocal).Callvirt(getReturnValue).StoreLocal(local[3]);
        }