示例#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());
        }
示例#2
0
        public static Type[] GetParamterTypes(this ActionSerDes actionSerDes, MethodInfo methodInfo = null)
        {
            IList <Type> paramterList = new List <Type>();

            if (actionSerDes != null && actionSerDes.ParamterInfoArray != null)
            {
                foreach (var item in actionSerDes.ParamterInfoArray)
                {
                    paramterList.Add(item.GetRType(methodInfo));
                }
            }
            return(paramterList.ToArray());
        }
示例#3
0
        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);
        }
        public static ActionSerDes ToActionSerDes(this ApiActionDescriptor apiActionDescriptor)
        {
            ActionSerDes actionSerDes = null;

            if (apiActionDescriptor != null)
            {
                actionSerDes = new ActionSerDes();
                actionSerDes.TargetTypeFullName = apiActionDescriptor.TargetTypeFullName;
                actionSerDes.MethodName         = apiActionDescriptor.Member.Name;
                if (apiActionDescriptor.Parameters != null)
                {
                    IList <ParamterInfoDes> paramterInfoDesList = new List <ParamterInfoDes>();
                    foreach (var item in apiActionDescriptor.Parameters)
                    {
                        ParamterInfoDes paramterInfoDes = item.ToParamInfo();
                        paramterInfoDesList.Add(paramterInfoDes);
                    }
                    actionSerDes.ParamterInfoArray = paramterInfoDesList.ToArray();
                    actionSerDes.RtnInfo           = apiActionDescriptor.Return.ToRtnInfo();
                }
            }
            return(actionSerDes);
        }
示例#5
0
        public static string GetRouteAddress(this ActionSerDes actionSerDes)
        {
            string routeAddress = null;

            if (actionSerDes != null)
            {
                int paramCount = 0;
                if (actionSerDes.ParamterInfoArray == null)
                {
                    actionSerDes.ParamterInfoArray = new ParamterInfoDes[0];
                }
                paramCount   = actionSerDes.ParamterInfoArray.Length;
                routeAddress = $"{actionSerDes.TargetTypeFullName}/{actionSerDes.MethodName}/";
                StringBuilder paramIdEntityBuilder = new StringBuilder();
                if (actionSerDes.ParamterInfoArray != null)
                {
                    for (int i = 0; i < actionSerDes.ParamterInfoArray.Length; i++)
                    {
                        var item = actionSerDes.ParamterInfoArray[i];
                        if (i == 0)
                        {
                            paramIdEntityBuilder.Append("(");
                        }
                        if (i == actionSerDes.ParamterInfoArray.Length - 1)
                        {
                            paramIdEntityBuilder.Append($"{item.TypeFullName}");
                            paramIdEntityBuilder.Append(")");
                        }
                        else
                        {
                            paramIdEntityBuilder.Append($"{item.TypeFullName},");
                        }
                    }
                }
            }
            return(routeAddress);
        }