//接收本地调用请求,然后转发远程访问 public override IMessage Invoke(IMessage msg) { try { IMethodCallMessage callMessage = (IMethodCallMessage)msg; MethodInfo methodInfo = (MethodInfo)callMessage.MethodBase; ParameterInfo[] parameterInfos = methodInfo.GetParameters(); Type returnType = methodInfo.ReturnType; IoMessage ioMessage = getIoMessage(type.ToString(), methodInfo.Name, callMessage.Args); //远程调用 object result = request(ioMessage); Debug.Log("返回了:" + result); //同步获取返回值 if (!returnType.ToString().Equals("System.Void")) { IoMessage message = (IoMessage)result; //如果不是基础数据类型 if (!returnType.IsPrimitive && !returnType.ToString().Equals("System.String")) { //反射ProtostuffUtils工具类 Assembly assembly = Assembly.GetExecutingAssembly(); // 获取当前程序集 object protoObj = assembly.CreateInstance("org.zgl.ProtostuffUtils"); // 创建类的实例,返回为 object 类 //获取工具类类型 Type protoType = protoObj.GetType(); //获取带有泛型反序列化的工具类方法 MethodInfo protoMethodInfo = protoType.GetMethod("ProtobufDeserialize").MakeGenericMethod(returnType); //反射调用泛型参数序列化返回序列化后的数据 object protoResult = protoMethodInfo.Invoke(protoObj, new object[] { message.getData() }); //返回最终返回值 return(returnmessage(callMessage, protoResult)); } else if (returnType.GetType().IsPrimitive || returnType.ToString().Equals("System.String")) { //如果是基础数据类型 object o = TypeExchange.exchange(returnType, message.getData().ToString()); return(returnmessage(callMessage, o)); } } else { //如果返回类型是void return(returnmessage()); } } catch (Exception e) { throw new NotImplementedException("远程调用异常", e); } return(null); }
/**都是基础类型的参数*/ private void baseIoMessageHandler(object obj, Type t, IoMessage ioMessage) { try { MethodInfo method = t.GetMethod(ioMessage.getMethodName()); ParameterInfo[] parameterInfo = method.GetParameters(); object[] args = null; if (parameterInfo.Length > 0) { string[] paramxx = ((string)ioMessage.getData()).Split('$');//先分割$字符串 for (int i = 0; i < parameterInfo.Length; i++) { args[i] = TypeExchange.exchange(parameterInfo[i].GetType(), paramxx[i]); } } method.Invoke(obj, args); } catch (Exception e) { Debug.LogError("反射异常"); } }