Beispiel #1
0
        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="fullPath">全路径</param>
        /// <param name="method">方法</param>
        /// <param name="parames">参数数组</param>
        /// <returns>返回值</returns>
        public virtual object Invoke(string fullPath, out MethodInfo method, params object[] parames)
        {
            string classFullPath;
            string methodName = ReflectUtil.GetMethodName(fullPath, out classFullPath);

            if (dicCaches.ContainsKey(classFullPath))
            {
                var insMapMethod = dicCaches[classFullPath];
                method = insMapMethod.GetMethodByName(methodName);
                if (method == null)
                {
                    method = insMapMethod.Instance.GetType().GetMethod(methodName);
                    insMapMethod.Methods.Add(method);
                }

                AutoEqualMethodParams(method, parames);

                return(method.Invoke(insMapMethod.Instance, parames));
            }
            else
            {
                var insMapMethod = new InstanceMapMethodsInfo()
                {
                    Instance = Instance.CreateInstance(classFullPath)
                };
                method = insMapMethod.Instance.GetType().GetMethod(methodName);
                insMapMethod.Methods.Add(method);

                Set(classFullPath, insMapMethod);

                AutoEqualMethodParams(method, parames);

                return(method.Invoke(insMapMethod.Instance, parames));
            }
        }
Beispiel #2
0
        /// <summary>
        /// 监听
        /// </summary>
        public void Listen()
        {
            RpcServer.Receive(inData =>
            {
                object result = null;
                try
                {
                    var rpcDataInfo = BytesSerialization.Deserialize <RpcDataInfo>(inData);
                    if (rpcDataInfo == null)
                    {
                        OnReceivingError("传过来的数据不是RpcDataInfo类型的");
                    }
                    else if (string.IsNullOrWhiteSpace(rpcDataInfo.MethodFullPath))
                    {
                        OnReceivingError("方法全路径不能为空");
                    }
                    else
                    {
                        string classFullName;
                        var methodName = ReflectUtil.GetMethodName(rpcDataInfo.MethodFullPath, out classFullName);

                        var implClassFullName = InterfaceMapImpl.Reader(classFullName);

                        MethodInfo method;
                        var methodReturnValue = MethodCall.Invoke(string.Format("{0}.{1}", implClassFullName, methodName), out method, rpcDataInfo.MethodParams);

                        // 如果方法返回是Void,则直接返回null
                        if (method.IsMethodReturnVoid())
                        {
                            return(null);
                        } // 如果方法是异步方法,则转换为Task并等待执行结束后返回Result给客户端
                        else if (method.ReturnType.IsTypeTask())
                        {
                            // 如果带泛型,则返回任务的Result给客户端
                            if (method.ReturnType.IsTypeGenericityTask())
                            {
                                var resultProperty = method.ReturnType.GetProperty("Result");
                                result             = resultProperty.GetValue(methodReturnValue);
                            }
                            else
                            {
                                var methodReturnTask = methodReturnValue as Task;
                                methodReturnTask.Wait();
                            }
                        }
                        else
                        {
                            result = methodReturnValue;
                        }
                    }

                    if (result == null)
                    {
                        return(null);
                    }

                    return(BytesSerialization.Serialize(result));
                }
                catch (Exception ex)
                {
                    try
                    {
                        OnReceivingError(ex.Message, ex);
                    }
                    catch { }

                    return(null);
                }
            });
        }
Beispiel #3
0
 public RpcInvocation(MethodInfo method, object[] arguments) : this(ReflectUtil.GetMethodName(method), method.GetParameters().Select(p => p.ParameterType).ToArray(), arguments)
 {
 }