示例#1
0
 public Invocation(string key, Type handlerTargetType, MethodInfo handlerMethodInfo, Type handlerParametersTupleType, IRpcSerializer rpcSerializer)
 {
     Key = key;
     HandlerTargetType         = handlerTargetType;
     HandlerMethodInfo         = handlerMethodInfo;
     HandlerParameterTupleType = handlerParametersTupleType;
     RpcSerializer             = rpcSerializer;
     _fastInvokeHandler        = DynamicProxyHelper.CreateFastInvokeHandler(HandlerMethodInfo);
 }
示例#2
0
        private static Type CreateProxyType()
        {
            var interfaceType = InterfaceType;
            var typeName      = string.Format("{0}_{1}_Imp_{2}", typeof(EasyProxyGenerator <T>).Name, interfaceType.Name, interfaceType.GetHashCode());

            TypeBuilder typeBuilder = ModuleBuilder.DefineType(typeName, TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed, typeof(ProxyBase));

            typeBuilder.AddInterfaceImplementation(interfaceType);

            MethodInfo   interceptMethodInfo = typeof(IInterceptor).GetMethod("Proceed", BindingFlags.Instance | BindingFlags.Public);
            FieldBuilder fieldBuilder        = DynamicProxyHelper.DefineField(typeBuilder, "interceptor", typeof(IInterceptor), FieldAttributes.Private);

            DynamicProxyHelper.DefineConstructor(typeBuilder, fieldBuilder);
            DynamicProxyHelper.DefineMethods(typeBuilder, fieldBuilder, interfaceType, interceptMethodInfo);

            return(typeBuilder.CreateType());
        }
示例#3
0
        private void RegisterService(string serviceName, string actionName, object service, Type serviceType, MethodInfo methodInfo, IRpcSerializer rpcSerializer)
        {
            var typeArray = DynamicProxyHelper.GetParameterTypes(methodInfo.GetParameters());

            if (typeArray.Length > 10)
            {
                throw new ArgumentException(string.Format("RegisterService error,service:{0},action:{1}, parameters count is not valid, max method parameter count is {2} ", serviceName, actionName, 10));
            }

            var parameterTupleType = CustomTuple.CreateType(typeArray);
            var title = DynamicProxyHelper.FormatTitle(serviceName, actionName, typeArray, methodInfo.ReturnType);

            var invocation = new Invocation(title, serviceType, methodInfo, parameterTupleType, rpcSerializer);

            _invocationDictionary.TryAdd(invocation.Key, invocation);

            LogAgent.Info(string.Format("RegisterService -> {0}", title));
        }
示例#4
0
        public static object CreateInstance(Type type, object[] parameterArray)
        {
            _createFuncDictionary.TryGetValue(type, out var _createFunc);
            if (_createFunc != null)
            {
                return(_createFunc(parameterArray));
            }

            var typeList = DynamicProxyHelper.GetParameterTypes(parameterArray);

            _createFuncDictionary[type] = CreateHandler(type, typeList);
            _createFuncDictionary.TryGetValue(type, out var _createFunc2);
            if (_createFunc2 != null)
            {
                return(_createFunc2(parameterArray));
            }

            throw new RpcException();
        }
示例#5
0
        private static MethodBuilder DefineMethod(MethodInfo interfaceMethodInfo, MethodInfo interceptMethodInfo, Type parameterTupleType, TypeBuilder typeBuilder, FieldBuilder fieldBuilder, string title)
        {
            ParameterInfo[] parameterArray = interfaceMethodInfo.GetParameters();

            MethodBuilder methodBuilder = typeBuilder.DefineMethod(interfaceMethodInfo.Name, DefaultMethodAttributes, interfaceMethodInfo.ReturnType, DynamicProxyHelper.GetParameterTypes(parameterArray));
            ILGenerator   il            = methodBuilder.GetILGenerator();

            il.DeclareLocal(typeof(object[]));

            if (interfaceMethodInfo.ReturnType != typeof(void))
            {
                il.DeclareLocal(interfaceMethodInfo.ReturnType);
            }

            il.Emit(OpCodes.Nop);
            il.Emit(OpCodes.Ldc_I4, parameterArray.Length);
            il.Emit(OpCodes.Newarr, typeof(object));
            il.Emit(OpCodes.Stloc_0);

            for (int i = 0; i < parameterArray.Length; i++)
            {
                il.Emit(OpCodes.Ldloc_0);
                il.Emit(OpCodes.Ldc_I4, i);
                il.Emit(OpCodes.Ldarg, 1 + i);
                il.Emit(OpCodes.Box, parameterArray[i].ParameterType);
                il.Emit(OpCodes.Stelem_Ref);
            }

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldfld, fieldBuilder);
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldstr, title);

            il.Emit(OpCodes.Ldtoken, parameterTupleType);

            il.Emit(OpCodes.Ldloc_0);
            il.Emit(OpCodes.Ldtoken, interfaceMethodInfo.ReturnType);
            il.Emit(OpCodes.Call, interceptMethodInfo);

            if (interfaceMethodInfo.ReturnType == typeof(void))
            {
                il.Emit(OpCodes.Pop);
            }
            else
            {
                il.Emit(OpCodes.Unbox_Any, interfaceMethodInfo.ReturnType);
                il.Emit(OpCodes.Stloc_1);
                il.Emit(OpCodes.Ldloc_1);
            }

            il.Emit(OpCodes.Ret);

            return(methodBuilder);
        }