private void DoMethodCall() { if (m_MethodCaller != null) { m_MethodCaller.Invoke(); } }
public object Invoke(object instance, object[] args) { return(MethodCall.Invoke(instance, args)); }
/// <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); } }); }
private void ExecuteCommand() { string sCommand = m_hCurrentCommand.ToString().Trim(); if (sCommand.Length == 0) { return; } MethodCall hMethod = null; object[] hParams; object hResult; IntPtr hAffinity = m_hCurrentProcess.ProcessorAffinity; ProcessPriorityClass eCurrentClass = m_hCurrentProcess.PriorityClass; ThreadPriority ePriority = Thread.CurrentThread.Priority; try { this.Parse(sCommand, out hMethod, out hParams); this.FillLine(sCommand, ConsoleColor.Yellow); m_hStopwatch.Reset(); m_hStopwatch.Start(); hResult = hMethod.Invoke(hParams); m_hStopwatch.Stop(); long lResult = m_hStopwatch.ElapsedMilliseconds; this.FillLine(sCommand, ConsoleColor.Green); Console.WriteLine(); if (hResult is IEnumerable && !(hResult is string)) { IEnumerable hCollection = hResult as IEnumerable; ConsoleUI.WriteLine(string.Format("Enumerating {0}", hResult.GetType().GetFriendlyName()), ConsoleColor.DarkGreen); Console.WriteLine(); int iCount = 0; foreach (object hItem in hCollection) { ConsoleUI.WriteLine(hItem.ToString(), ConsoleColor.DarkGreen); iCount++; } ConsoleUI.WriteLine(string.Format("{0} Elements", iCount), ConsoleColor.DarkGreen); } else { if (hResult != null) { ConsoleUI.WriteLine(hResult.ToString(), ConsoleColor.DarkGreen); } } ConsoleUI.WriteLine(string.Format("{0} Ms", lResult), ConsoleColor.DarkGreen); m_hSoundEmitter.BeepSuccess(); } catch (ArgumentException) { this.FillLine(sCommand, ConsoleColor.Red); Console.WriteLine(); ConsoleUI.WriteLine("Bad Arguments, check function signature:", ConsoleColor.DarkRed); ConsoleUI.WriteLine(hMethod.Signature, ConsoleColor.DarkRed); m_hSoundEmitter.BeepError(); } catch (TargetInvocationException hEx) { this.FillLine(sCommand, ConsoleColor.Red); Console.WriteLine(); ConsoleUI.WriteLine(hEx.InnerException.ToString(), ConsoleColor.DarkRed); m_hSoundEmitter.BeepError(); } catch (Exception hEx) { this.FillLine(sCommand, ConsoleColor.Red); Console.WriteLine(); ConsoleUI.WriteLine(hEx.ToString(), ConsoleColor.DarkRed); m_hSoundEmitter.BeepError(); } finally { m_hCurrentProcess.ProcessorAffinity = hAffinity; m_hCurrentProcess.PriorityClass = eCurrentClass; Thread.CurrentThread.Priority = ePriority; m_hHistoryList.Add(sCommand); m_iHistoryIndex = -1; Console.CursorLeft = 0; m_hCurrentCommand = new StringBuilder(); this.ResetAutocompletion(); Console.WriteLine(); } }