コード例 #1
0
ファイル: ConvertUtils.cs プロジェクト: haibo-chen/Security
        public static Object strToObject(this String x, Type destType, String format = "", Properties props = null)
        {
            if (x == null || x == "")
            {
                return(null);
            }

            //基础数据类型转换
            if (destType == typeof(char))
            {
                return((Object)x[0]);
            }
            else if (destType == typeof(int) || destType == typeof(Int32))
            {
                return((Object)strToInt(x, format, props));
            }
            else if (destType == typeof(float) || destType == typeof(double))
            {
                return((Object)strTodouble(x, format, props));
            }
            else if (destType == typeof(String))
            {
                return((Object)x);
            }
            else if (destType == typeof(DateTime))
            {
                return((Object)strToDatetime(x, format, props));
            }
            else if (destType.IsEnum)
            {
                return(Enum.Parse(destType, x));
            }
            else if (destType.IsArray)
            {
                String[] sArray = x.Split(',');
                if (sArray == null || sArray.Length <= 0)
                {
                    return(null);
                }
                Type  eleType = destType.GetElementType();
                Array inst    = Array.CreateInstance(eleType, sArray.Length);
                for (int i = 0; i < sArray.Length; i++)
                {
                    Object t = ReflectionUtils.CallGenericMethod(typeof(ConvertUtils), null, "strToObject", new Type[] { eleType }, new Object[] { sArray[i], format, props });
                    inst.SetValue(t, i);
                }
                return((Object)inst);
            }
            else if (typeof(IEnumerable).IsAssignableFrom(destType))
            {
                String[] sArray = x.Split(',');
                if (sArray == null || sArray.Length <= 0)
                {
                    return(null);
                }
                Type eleType = destType.GetGenericArguments()[0];

                Type listType = typeof(List <>);
                listType = listType.MakeGenericType(eleType);

                //IList list = (IList)listType.GetConstructor(null).Invoke(null);
                IList list = (IList)listType.Assembly.CreateInstance(listType.FullName);
                for (int i = 0; i < sArray.Length; i++)
                {
                    Object t = ReflectionUtils.CallGenericMethod(typeof(ConvertUtils), null, "strToObject", new Type[] { eleType }, new Object[] { sArray[i], format, props });
                    list.Add(t);
                }
                return((Object)list);
            }

            Object     obj         = destType.Assembly.CreateInstance(destType.FullName);
            MethodInfo parseMethod = destType.GetMethod("Parse", new Type[] { typeof(String), typeof(String), typeof(Properties) });

            if (parseMethod != null && parseMethod.IsStatic)
            {
                return(parseMethod.Invoke(obj, new Object[] { x, format, props }));
            }


            if (props == null)
            {
                props = new Properties();
            }
            String sep = props.Get <String>(TEXT_PROP_SEP, ",");

            if (format == null || format == "")
            {
                format = props.Get <String>(TEXT_PROP_FORMAT, TEXT_FMT_DEFAULT);
            }
            String propNames = props.Get <String>(TEXT_PROP_PROPNAMES, "");

            PropertyInfo[]      memberInfos = destType.GetProperties();
            List <PropertyInfo> members     = new List <PropertyInfo>(memberInfos);

            String[] ss = x.Split(sep.ToCharArray());
            if (ss == null || ss.Length <= 0)
            {
                return(null);
            }

            for (int i = 0; i < ss.Length; i++)
            {
                if (ss[i] == null)
                {
                    ss[i] = "";
                }
                String ttStr = "";
                String tvStr = ss[i].Trim();
                if (ss[i].Contains("="))
                {
                    ttStr = ss[i].Split('=')[0].Trim();
                    tvStr = ss[i].Split('=')[1].Trim();
                }
                PropertyInfo member = members.FirstOrDefault(m => m.HasName(ttStr));
                if (member == null)
                {
                    member = members[i];
                }

                Object value = ConvertTo(ss[i], member.PropertyType);
                if (member.GetSetMethod() != null)
                {
                    member.SetValue(obj, value);
                }
            }
            return(obj);
        }