/// <summary> /// /// </summary> /// <returns></returns> public GameEntityT CreateNewInstance <GameEntityT>() where GameEntityT : WorldEntity { if (m_ConstructorInstance != null) { object returnObject = m_ConstructorInstance.Invoke(); GameEntityT creature = returnObject as GameEntityT; if (creature == null) { LOGs.WriteLine(LogMessageType.MSG_ERROR, "GameEntity.CreateNewInstance(...) - creature == null error!"); return(null); } return(creature); } else { m_ConstructorInstance = DynamicCalls.GetInstanceCreator(GetType()); if (m_ConstructorInstance == null) { throw new Exception("GameEntity.CreateNewInstance(...) - m_ConstructorInstance == null error!"); } GameEntityT creature = m_ConstructorInstance.Invoke() as GameEntityT; if (creature == null) { LOGs.WriteLine(LogMessageType.MSG_ERROR, "GameEntity.CreateNewInstance(...) - creature == null error!"); return(null); } return(creature); } }
/// <summary> /// 方法调用 /// </summary> /// <param name="kind"></param> /// <param name="method"></param> /// <param name="parameters"></param> /// <returns></returns> public object Invoke(string kind, string method, NameValueCollection parameters, out Type retType) { WebOperationContext context = WebOperationContext.Current; BusinessMethodModel metadata = pool.FindMethod(kind, method); //返回类型 retType = metadata.Method.ReturnType; //处理请求类型 if (metadata.HttpMethod == HttpMethod.POST && context.IncomingRequest.Method.ToUpper() == "GET") { throw new RESTfulException((int)HttpStatusCode.MethodNotAllowed, "Resources can only by the [" + metadata.HttpMethod + "] way to acquire."); } //实例对象 object instance = null; try { //调用方法 object[] arguments = ParameterHelper.Convert(metadata.Parameters, parameters); instance = register.Resolve(metadata.Service); return(DynamicCalls.GetMethodInvoker(metadata.Method)(instance, arguments)); } finally { register.Release(instance); } }
/// <summary> /// 消息回调 /// </summary> /// <param name="sender"></param> /// <param name="args"></param> void reqService_OnCallback(object sender, ServiceMessageEventArgs args) { if (args.Result is ResponseMessage) { var resMsg = args.Result as ResponseMessage; base.QueueMessage(resMsg); } else if (args.Result is CallbackMessage) { var callbackMsg = args.Result as CallbackMessage; if (callback != null) { var callbackType = callback.GetType(); //获取接口的类型 var interfaces = callbackType.GetInterfaces(); if (interfaces.Length > 0) { //判断类型是否相同 if (interfaces.Any(type => type.FullName == callbackMsg.ServiceName)) { var method = CoreHelper.GetMethodFromType(callbackType, callbackMsg.MethodName); //执行委托 DynamicCalls.GetMethodInvoker(method).Invoke(callback, callbackMsg.Parameters); } } } } }
/// <summary> /// 设置属性值 /// </summary> /// <param name="obj"></param> /// <param name="property"></param> /// <param name="value"></param> public static void SetPropertyValue(object obj, PropertyInfo property, object value) { if (property.CanWrite) { var propertySetter = DynamicCalls.GetPropertySetter(property); value = ConvertValue(property.PropertyType, value); propertySetter(obj, value); } }
/// <summary> /// /// </summary> /// <param name="type"></param> /// <param name="fProbability"></param> public Loot(Type type, float fProbability) { m_ConstructorInstance = DynamicCalls.GetInstanceCreator(type); if (m_ConstructorInstance == null) { throw new Exception("Loot.Loot(...) - m_ConstructorInstance == null error!"); } m_fProbability = fProbability; }
public void GetMethodInvokerTest() { MethodInfo methodInfo = null; // TODO: 初始化为适当的值 FastInvokeHandler expected = null; // TODO: 初始化为适当的值 FastInvokeHandler actual; actual = DynamicCalls.GetMethodInvoker(methodInfo); Assert.AreEqual(expected, actual); Assert.Inconclusive("验证此测试方法的正确性。"); }
public void GetPropertyGetterTest() { PropertyInfo propInfo = null; // TODO: 初始化为适当的值 FastPropertyGetHandler expected = null; // TODO: 初始化为适当的值 FastPropertyGetHandler actual; actual = DynamicCalls.GetPropertyGetter(propInfo); Assert.AreEqual(expected, actual); Assert.Inconclusive("验证此测试方法的正确性。"); }
public void GetInstanceCreatorTest() { Type type = null; // TODO: 初始化为适当的值 FastCreateInstanceHandler expected = null; // TODO: 初始化为适当的值 FastCreateInstanceHandler actual; actual = DynamicCalls.GetInstanceCreator(type); Assert.AreEqual(expected, actual); Assert.Inconclusive("验证此测试方法的正确性。"); }
public PropertyHandler(PropertyInfo property) { if (property.CanWrite) { this.mSetValue = DynamicCalls.GetPropertySetter(property); } if (property.CanRead) { this.mGetValue = DynamicCalls.GetPropertyGetter(property); } this.mProperty = property; }
/// <summary> /// 通过字符串的方式来执行方法 /// </summary> /// <param name="obj">方法所在对象</param> /// <param name="name">方法名称</param> /// <param name="values">方法对应的参数值</param> /// <returns></returns> protected object RunMethod(object obj, string name, params object[] values) { Type type = obj.GetType(); MethodInfo method = type.GetMethod(name, BindingFlags.Instance | BindingFlags.Public); if (method == null) { return(null); } try { FastInvokeHandler handler = DynamicCalls.GetMethodInvoker(method); return(handler.Invoke(obj, values)); } catch { return(null); } }
/// <summary> /// /// </summary> /// <param name="invokeObject"></param> /// <param name="MethodName"></param> /// <returns></returns> private AjaxCallbackParam InvokeMethod(object invokeObject, string MethodName) { var ajaxMethods = AjaxMethodHelper.GetAjaxMethods(invokeObject.GetType()); if (ajaxMethods.ContainsKey(MethodName)) { var parameters = ajaxMethods[MethodName].Method.GetParameters(); var plist = new List <object>(); foreach (var p in parameters) { object obj = GetObject(p.ParameterType, p.Name); plist.Add(obj); } var method = ajaxMethods[MethodName].Method; var handler = DynamicCalls.GetMethodInvoker(method); object value = handler.Invoke(invokeObject, plist.ToArray()); return(new AjaxCallbackParam(value)); } else { throw new AjaxException(string.Format("未找到服务器端方法{0}!", MethodName)); } }
/// <summary> /// Runs the specified MSG. /// </summary> /// <param name="reqMsg">The MSG.</param> /// <returns>The msg.</returns> protected override ResponseMessage Run(RequestMessage reqMsg) { var resMsg = new ResponseMessage { TransactionId = reqMsg.TransactionId, ReturnType = reqMsg.ReturnType, ServiceName = reqMsg.ServiceName, MethodName = reqMsg.MethodName }; #region 获取相应的方法 var methodKey = string.Format("{0}${1}", reqMsg.ServiceName, reqMsg.MethodName); if (!hashtable.ContainsKey(methodKey)) { var m = CoreHelper.GetMethodFromType(serviceType, reqMsg.MethodName); if (m == null) { string message = string.Format("The server【{2}({3})】not find matching method. ({0},{1})." , reqMsg.ServiceName, reqMsg.MethodName, DnsHelper.GetHostName(), DnsHelper.GetIPAddress()); resMsg.Error = new WarningException(message); return(resMsg); } hashtable[methodKey] = m; } #endregion //容器实例对象 object instance = null; try { //定义Method var method = hashtable[methodKey] as System.Reflection.MethodInfo; //解析服务 instance = container.Resolve(serviceType); //返回拦截服务 var service = AspectFactory.CreateProxyService(serviceType, instance); if (reqMsg.InvokeMethod) { var objValue = reqMsg.Parameters["InvokeParameter"]; var jsonString = (objValue == null ? string.Empty : objValue.ToString()); //解析参数 reqMsg.Parameters = IoCHelper.CreateParameters(method, jsonString); } //参数赋值 object[] parameters = IoCHelper.CreateParameterValues(method, reqMsg.Parameters); //调用对应的服务 resMsg.Value = DynamicCalls.GetMethodInvoker(method).Invoke(service, parameters); //处理返回参数 IoCHelper.SetRefParameters(method, resMsg.Parameters, parameters); //返回结果数据 if (reqMsg.InvokeMethod) { resMsg.Value = new InvokeData { Value = SerializationManager.SerializeJson(resMsg.Value), Count = resMsg.Count, OutParameters = resMsg.Parameters.ToString() }; //清除参数集合 resMsg.Parameters.Clear(); } } catch (Exception ex) { //捕获全局错误 resMsg.Error = ex; } finally { //释放资源 container.Release(instance); instance = null; } return(resMsg); }
/// <summary> /// 快速实例化一个FastCreateInstanceHandler /// </summary> /// <param name="type"></param> /// <returns></returns> public static FastCreateInstanceHandler Create(Type type) { return(DynamicCalls.GetInstanceCreator(type)); }
/// <summary> /// 快速执行Method /// </summary> /// <param name="obj"></param> /// <param name="method"></param> /// <param name="parameters"></param> /// <returns></returns> public static object FastMethodInvoke(object obj, MethodInfo method, params object[] parameters) { return(DynamicCalls.GetMethodInvoker(method)(obj, parameters)); }