示例#1
0
 /// <summary>
 /// 返回方法执行所需要的参数
 /// </summary>
 /// <param name="method">要执行的方法</param>
 /// <param name="p">传递来的参数</param>
 /// <returns></returns>
 private static object[] getInvokeParam(MethodInfo method, Letter p)
 {
     ParameterInfo[] paramInfos = method.GetParameters();
     object[]        objs       = new object[paramInfos.Length];
     for (int i = 0; i < objs.Length; i++)
     {
         ParameterInfo pi = paramInfos[i];
         //如果参数是Parameter类型,则直接赋值
         if (p.GetType().FullName.Equals(pi.ParameterType.FullName))
         {
             objs[i] = p;
         }
         else
         {
             //普通参数,不是输出参数
             if (!pi.IsOut)
             {
                 object val = p[pi.Name].String;
                 objs[i] = Convert.ChangeType(val, pi.ParameterType);
             }
             else
             {
                 objs[i] = null;
             }
         }
     }
     return(objs);
 }
示例#2
0
        /// <summary>
        /// 返回方法执行所需要的参数
        /// </summary>
        /// <param name="method">要执行的方法</param>
        /// <param name="letter">传递来的参数</param>
        /// <returns></returns>
        private static object[] getInvokeParam(MethodInfo method, Letter letter)
        {
            ParameterInfo[] paramInfos = method.GetParameters();
            object[]        objs       = new object[paramInfos.Length];
            for (int i = 0; i < objs.Length; i++)
            {
                ParameterInfo pi = paramInfos[i];
                //如果参数是Letter类型,则直接赋值
                if (letter.GetType().FullName.Equals(pi.ParameterType.FullName))
                {
                    objs[i] = letter;
                }
                else
                {
                    //普通参数,不是输出参数
                    if (!pi.IsOut)
                    {
                        string val = letter[pi.Name].String;
                        if (!pi.ParameterType.Name.Equals("string", StringComparison.CurrentCultureIgnoreCase))
                        {
                            if (string.IsNullOrWhiteSpace(val))
                            {
                                throw new Exception("参数 " + pi.Name + " 的值为空");
                            }
                        }
                        try
                        {
                            switch (pi.ParameterType.Name)
                            {
                            case "DateTime":
                                if (val == null || string.IsNullOrWhiteSpace(val.ToString()))
                                {
                                    objs[i] = DateTime.Now;
                                    break;
                                }
                                DateTime dt    = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                                long     lTime = long.Parse(val + "0000");
                                TimeSpan toNow = new TimeSpan(lTime);
                                objs[i] = dt.Add(toNow);
                                break;

                            default:
                                objs[i] = Convert.ChangeType(val, pi.ParameterType);
                                break;
                            }
                            //objs[i] = Convert.ChangeType(val, pi.ParameterType);
                        }
                        catch
                        {
                            throw new Exception("参数 " + pi.Name + " 的值,数据格式不正确");
                        }
                    }
                    else
                    {
                        objs[i] = null;
                    }
                }
            }
            return(objs);
        }
示例#3
0
 /// <summary>
 /// 返回方法执行所需要的参数
 /// </summary>
 /// <param name="method">要执行的方法</param>
 /// <param name="letter">传递来的参数</param>
 /// <returns></returns>
 private static object[] getInvokeParam(MethodInfo method, Letter letter)
 {
     ParameterInfo[] paramInfos = method.GetParameters();
     object[]        objs       = new object[paramInfos.Length];
     for (int i = 0; i < objs.Length; i++)
     {
         ParameterInfo pi = paramInfos[i];
         //如果形参是Letter类型,则直接赋值
         if (letter.GetType().FullName.Equals(pi.ParameterType.FullName))
         {
             objs[i] = letter;
             continue;
         }
         //如果形参为输出型的,则不赋值(ViewData接口不允许此类参数)
         if (pi.IsOut)
         {
             objs[i] = null;
             continue;
         }
         //实参的值,即接口方法的参数所对应的客户端传来的值
         string val = letter[pi.Name].String;
         if (!pi.ParameterType.Name.Equals("string", StringComparison.CurrentCultureIgnoreCase))
         {
             if (!pi.ParameterType.Name.Equals("int32", StringComparison.CurrentCultureIgnoreCase))
             {
                 if (string.IsNullOrWhiteSpace(val))
                 {
                     throw new Exception("参数 " + pi.Name + " 的值为空");
                 }
             }
             //if (string.IsNullOrWhiteSpace(val)) throw new Exception("参数 " + pi.Name + " 的值为空");
         }
         //如果形参是数据实体
         if (pi.ParameterType.BaseType != null && pi.ParameterType.BaseType.FullName == "WeiSha.Data.Entity")
         {
             objs[i] = _getValueToEntity <WeiSha.Data.Entity>(pi.ParameterType, val);
             continue;
         }
         //如果形参是数组
         if (pi.ParameterType.IsArray)
         {
             objs[i] = _getValueToArray(pi.ParameterType, val);
             continue;
         }
         try
         {
             objs[i] = _getValueToObject <object>(pi.ParameterType, val);
         }
         catch
         {
             throw new Exception("参数 " + pi.Name + " 的值,数据格式不正确");
         }
     }
     return(objs);
 }
示例#4
0
        /// <summary>
        /// 根据方法名、参数数量,获取具体要执行的方法
        /// </summary>
        /// <param name="type"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        private static MethodInfo getMethod(Type type, Letter p)
        {
            //1、先将名称匹配的方法收集起来
            List <MethodInfo> list = new List <MethodInfo>();

            MethodInfo[] methods = type.GetMethods();
            for (int i = 0; i < methods.Length; i++)
            {
                if (p.MethodName.Equals(methods[i].Name, StringComparison.CurrentCultureIgnoreCase))
                {
                    list.Add(methods[i]);
                }
            }
            if (list.Count < 1)
            {
                throw new Exception(string.Format("调用方法'{0}.{1}'不存在", p.ClassName, p.MethodName));
            }
            //2、判断方法的参数名称,是否与传递来的参数名称匹配
            //并不要求两者的参数数量相同,也不要求数据类型相同
            MethodInfo method = null;

            foreach (MethodInfo mi in list)
            {
                //ParameterInfo[] paramInfos = mi.GetParameters();
                bool ismatch = true;    //是否匹配
                foreach (ParameterInfo pi in mi.GetParameters())
                {
                    //如果参数是Parameter类型,则跳过匹配
                    if (p.GetType().FullName.Equals(pi.ParameterType.FullName))
                    {
                        continue;
                    }
                    //只要有一个参数不匹配,即中断
                    if (!p.ExistParameter(pi.Name))
                    {
                        ismatch = false;
                        break;
                    }
                }
                if (ismatch)
                {
                    method = mi;
                }
            }
            if (method == null)
            {
                throw new Exception(
                          string.Format("所调用方法'{0}.{1}'的参数名称与实际传参不匹配;实际传参:{2}",
                                        type.FullName, p.MethodName,
                                        p.ToString() == string.Empty ? "null" : p.ToString()));
            }
            return(method);
        }
示例#5
0
        /// <summary>
        /// 要执行的方法,根据方法名、参数数量
        /// </summary>
        /// <param name="type">要调用的对象的类型</param>
        /// <param name="p"></param>
        /// <returns></returns>
        private static MethodInfo getMethod(Type type, Letter p)
        {
            //1、先判断方法是否存在
            List <MethodInfo> methods = new List <MethodInfo>();

            foreach (MethodInfo mi in type.GetMethods())
            {
                if (p.MethodName.Equals(mi.Name, StringComparison.CurrentCultureIgnoreCase))
                {
                    methods.Add(mi);
                }
            }
            if (methods.Count < 1)
            {
                throw new Exception(string.Format("调用方法'{0}.{1}'不存在", p.ClassName, p.MethodName));
            }
            //2、判断方法的参数名称,是否与传递来的参数名称匹配,参数数量必须匹配
            //只有一个参数,且类型是Letter
            MethodInfo mbLetter = null;

            for (int i = 0; i < methods.Count; i++)
            {
                ParameterInfo[] pis = methods[i].GetParameters();
                if (pis.Length == 1 && p.GetType().FullName.Equals(pis[0].ParameterType.FullName))
                {
                    mbLetter = methods[i];
                    methods.Remove(methods[i]);
                    break;
                }
            }
            MethodInfo method = null;

            foreach (MethodInfo mi in methods)
            {
                //2-1、判断参数个数是否相同
                int paraCount = 0;
                foreach (ParameterInfo pi in mi.GetParameters())
                {
                    if (!pi.IsOut)
                    {
                        paraCount++;
                    }
                }
                //方法的参数个数,和传参的参数个数,必须相等
                if (paraCount == p.Params.Count)
                {
                    method = mi;
                    break;
                }
            }
            //2-2、判断参数名称与传递来的参数名称是否一致
            if (method != null)
            {
                bool ismatch = true;    //是否匹配
                foreach (ParameterInfo pi in method.GetParameters())
                {
                    //如果参数是Parameter类型,则跳过匹配
                    if (p.GetType().FullName.Equals(pi.ParameterType.FullName))
                    {
                        continue;
                    }
                    //只要有一个参数不匹配,即中断
                    if (!p.ExistParameter(pi.Name))
                    {
                        ismatch = false;
                        break;
                    }
                }
                if (!ismatch)
                {
                    method = null;
                }
            }
            if (method == null)
            {
                method = mbLetter;
            }
            if (method == null)
            {
                throw new Exception(
                          string.Format("所调用方法'{0}.{1}'的参数名称与实际传参不匹配;实际传参:{2}",
                                        type.FullName, p.MethodName,
                                        p.ToString() == string.Empty ? "null" : p.ToString()));
            }
            return(method);
        }
示例#6
0
        /// <summary>
        /// 返回方法执行所需要的参数
        /// </summary>
        /// <param name="method">要执行的方法</param>
        /// <param name="letter">传递来的参数</param>
        /// <returns></returns>
        private static object[] getInvokeParam(MethodInfo method, Letter letter)
        {
            ParameterInfo[] paramInfos = method.GetParameters();
            object[]        objs       = new object[paramInfos.Length];
            for (int i = 0; i < objs.Length; i++)
            {
                ParameterInfo pi = paramInfos[i];
                //如果参数是Letter类型,则直接赋值
                if (letter.GetType().FullName.Equals(pi.ParameterType.FullName))
                {
                    objs[i] = letter;
                    continue;
                }
                //接口方法的参数所对应的客户端传来的值
                string val = letter[pi.Name].String;
                //如果参数为输出型的,则不赋值(ViewData接口不允许此类参数)
                if (pi.IsOut || string.IsNullOrWhiteSpace(val))
                {
                    objs[i] = null;
                    continue;
                }
                //如果参数是数据实体
                if (pi.ParameterType.BaseType != null && pi.ParameterType.BaseType.FullName == "WeiSha.Data.Entity")
                {
                    //创建实体
                    object entity = pi.ParameterType.Assembly.CreateInstance(pi.ParameterType.FullName);
                    Dictionary <string, string> dic = JsonConvert.DeserializeObject <Dictionary <string, string> >(val);
                    PropertyInfo[] props            = pi.ParameterType.GetProperties();
                    for (int j = 0; j < props.Length; j++)
                    {
                        PropertyInfo opi = props[j];    //实体属性
                        foreach (KeyValuePair <string, string> kv in dic)
                        {
                            if (kv.Key.Equals(opi.Name, StringComparison.CurrentCultureIgnoreCase))
                            {
                                //实体属性的值
                                object tm = string.IsNullOrEmpty(kv.Value) ? null : WeiSha.Common.DataConvert.ChangeType(kv.Value, opi.PropertyType);
                                opi.SetValue(entity, tm, null);
                                continue;
                            }
                        }
                    }
                    objs[i] = entity;
                    continue;
                }
                //将客户端传来的参数,转换为方法对应的参数类型

                if (!pi.ParameterType.Name.Equals("string", StringComparison.CurrentCultureIgnoreCase))
                {
                    if (string.IsNullOrWhiteSpace(val))
                    {
                        throw new Exception("参数 " + pi.Name + " 的值为空");
                    }
                }
                try
                {
                    switch (pi.ParameterType.Name)
                    {
                    case "DateTime":
                        if (val == null || string.IsNullOrWhiteSpace(val.ToString()))
                        {
                            objs[i] = DateTime.Now;
                            break;
                        }
                        DateTime dt    = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                        long     lTime = long.Parse(val + "0000");
                        TimeSpan toNow = new TimeSpan(lTime);
                        objs[i] = dt.Add(toNow);
                        break;

                    default:
                        objs[i] = Convert.ChangeType(val, pi.ParameterType);
                        break;
                    }
                }
                catch
                {
                    throw new Exception("参数 " + pi.Name + " 的值,数据格式不正确");
                }
            }
            return(objs);
        }