Ejemplo n.º 1
0
        private void EmitMakeCall(ILGenerator ilGenerator, MethodInfo method, FieldBuilder callService, bool compressRequest, bool compressResponse)
        {
            MethodInfo methodToCall;

            if (method.ReturnType == typeof(void))
            {
                methodToCall = typeof(IRpcProxyService).GetMethod("MakeCallNoReturn");
            }
            else if (method.ReturnType == typeof(Task))
            {
                methodToCall = typeof(IRpcProxyService).GetMethod("MaskAsyncCallNoReturn");
            }
            else if (method.ReturnType.IsConstructedGenericType &&
                     method.ReturnType.GetGenericTypeDefinition() == typeof(Task <>))
            {
                methodToCall =
                    typeof(IRpcProxyService).GetMethod("MakeAsyncCallWithReturn")
                    .MakeGenericMethod(method.ReturnType.GenericTypeArguments);
            }
            else
            {
                methodToCall = typeof(IRpcProxyService).GetMethod("MakeCallWithReturn").MakeGenericMethod(method.ReturnType);
            }

            ilGenerator.Emit(OpCodes.Ldarg_0);
            ilGenerator.Emit(OpCodes.Ldfld, callService);
            ilGenerator.Emit(OpCodes.Ldstr, method.DeclaringType.Namespace);
            ilGenerator.Emit(OpCodes.Ldstr, _namingConventionService.GetNameForType(method.DeclaringType));
            ilGenerator.Emit(OpCodes.Ldstr, _namingConventionService.GetMethodName(method));
            ilGenerator.Emit(OpCodes.Ldloc_0);
            ilGenerator.EmitInt(compressRequest ? 1 : 0);
            ilGenerator.EmitInt(compressResponse ? 1 : 0);
            ilGenerator.Emit(OpCodes.Callvirt, methodToCall);

            ilGenerator.Emit(OpCodes.Ret);
        }