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)); }
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(); }
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); }