Exemple #1
0
        public static object[] GetParamters(this ActionSerDes actionSerDes, MethodInfo methodInfo = null)
        {
            IList <object> paramterList = new List <object>();

            if (actionSerDes != null && actionSerDes.ParamterInfoArray != null)
            {
                foreach (var item in actionSerDes.ParamterInfoArray)
                {
                    Type   itemType  = item.GetRType(methodInfo);
                    object itemValue = item.Value;
                    if (itemType.IsValueType)
                    {
                        //值类型
                        itemValue = Convert.ChangeType(item.Value, itemType);
                    }
                    else if (itemValue != null)
                    {
                        JContainer jResult = itemValue as JContainer;
                        if (jResult != null)
                        {
                            MethodInfo _JsonMethod = typeof(SerializationUtility).GetMethods().FirstOrDefault(f => f.Name == "JsonToObject" && f.IsGenericMethodDefinition);
                            _JsonMethod = _JsonMethod.MakeGenericMethod(itemType);
                            //itemValue= _JsonMethod.Invoke(null,new object[] { jResult.ToString() });
                            var dynamicDelegate = DynamicMethodTool.GetMethodInvoker(_JsonMethod);
                            itemValue = dynamicDelegate(null, new object[] { jResult.ToString() });
                        }
                    }
                    paramterList.Add(itemValue);
                }
            }
            return(paramterList.ToArray());
        }
        public object CallAction(ActionSerDes actionDes)
        {
            MethodInfo callMethodInfo = null;
            Type       actionType     = ConstractInterfaceCache.Get(actionDes.TargetTypeFullName);
            dynamic    serviceValue   = IocUnity.Get(actionType);
            string     actionKey      = actionDes.GetRouteAddress();

            callMethodInfo = ActionMethodInfoCache.GetOrAdd(actionKey, key =>
            {
                var sameNameMethodList = actionType.GetMethods(BindingFlags.Instance | BindingFlags.Public).Where(f => f.Name == actionDes.MethodName).ToList();
                MethodInfo methodInfo  = null;
                //只区分参数个数不区分类型
                if (sameNameMethodList.Count == 1)
                {
                    methodInfo = sameNameMethodList.FirstOrDefault();
                }
                else
                {
                    methodInfo = sameNameMethodList.FirstOrDefault(f => f.GetParameters().Length == actionDes.ParamterInfoArray.Length);
                }
                return(methodInfo);
            });
            if (callMethodInfo == null)
            {
                throw new KeyNotFoundException($"路由地址没有找到【{actionKey}】!");
            }

            var    actionParamters = actionDes.GetParamters(callMethodInfo);
            object rtnObj          = null;

            try
            {
                var dynamicDelegate = DynamicMethodTool.GetMethodInvoker(callMethodInfo);
                rtnObj = dynamicDelegate(serviceValue, actionParamters);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                throw;
            }

            return(rtnObj);
        }