static public object GetT(Type type, JsonData jd)
        {
            string realType = JsonTools.GetDataType(type);

            if (realType == DataType.OBJECT)
            {
                return(GetObjT(type, jd));
            }
            else if (realType == DataType.ARRAY)
            {
                if (type.IsGenericType)
                {
                    Type   listType = type.GetMethod("Find").ReturnType;
                    object orgList  = Activator.CreateInstance(type);

                    for (int j = 0; j < jd.Count; j++)
                    {
                        object lo = GetT(listType, jd.Get(j));
                        if (lo != null)
                        {
                            MethodInfo m = orgList.GetType().GetMethod("Add");
                            m.Invoke(orgList, new object[] { lo });
                        }
                    }

                    return(orgList);
                }
                else if (type.BaseType == typeof(Array))
                {
                    MethodInfo[] ms       = type.GetMethods();
                    Type         listType = type.GetElementType();
                    object       orgList  = Array.CreateInstance(listType, jd.Count);
                    MethodInfo   sv       = type.GetMethod("SetValue", new Type[2] {
                        typeof(object), typeof(int)
                    });
                    for (int j = 0; j < jd.Count; j++)
                    {
                        object lo = GetT(listType, jd.Get(j));
                        if (lo != null)
                        {
                            sv.Invoke(orgList, new object[] { lo, j });
                        }
                    }

                    return(orgList);
                }
            }
            else if (realType == DataType.BOOL)
            {
                bool bv = jd.ToString().ToLower() == "true" ? true : false;
                return(bv);
            }
            else if (realType == DataType.STRING)
            {
                return(jd.ToString());
            }
            else if (realType == DataType.NUMBER)
            {
                string sv = jd.ToString();
                if (type == typeof(int))
                {
                    return(int.Parse(sv));
                }
                else if (type == typeof(float))
                {
                    double dsv = Convert.ToDouble(sv);
                    float  fsv = Convert.ToSingle(dsv);
                    return(fsv);
                }
                else if (type == typeof(double))
                {
                    return(double.Parse(sv));
                }
                else if (type == typeof(Single))
                {
                    return(Single.Parse(sv));
                }
                else if (type == typeof(long))
                {
                    return(long.Parse(sv));
                }
            }
            else if (realType == DataType.DICTIONARY)
            {
                object orgList = Activator.CreateInstance(type);
                string str     = type.FullName;
                string regex   = @"(?<=Dictionary`2\[).+(?=\])";
                string match   = StringTools.GetFirstMatch(str, regex);
                regex = @"\[[^\[\]]*(((?'Open'\[)[^\[\]]*)+((?'-Open'\])[^\[\]]*)+)*(?(Open)(?!))\]";
                string[]    matchs   = StringTools.GetAllMatchs(match, regex);
                List <Type> keyTypes = new List <Type>();
                for (int j = 0; j < matchs.Length; j++)
                {
                    if (matchs[j].IndexOf("Dictionary") < 0)
                    {
                        regex = @"(?<=\[)[^,]+(?=,)";
                        match = StringTools.GetFirstMatch(matchs[j], regex);
                        keyTypes.Add(Type.GetType(match, true, true));
                    }
                    else
                    {
                        regex = @"(?<=Dictionary`2\[).+(?=\])";
                        match = StringTools.GetFirstMatch(matchs[j], regex);
                        regex = @"(?<=\[)[^,]+(?=,)";
                        string[] subMatchs = StringTools.GetAllMatchs(match, regex);
                        if (subMatchs.Length >= 2)
                        {
                            Type        t1      = Type.GetType(subMatchs[0]);
                            Type        t2      = Type.GetType(subMatchs[1]);
                            IDictionary subDict = (IDictionary)Activator.CreateInstance(typeof(Dictionary <,>).MakeGenericType(t1, t2));
                            keyTypes.Add(subDict.GetType());
                        }
                    }
                }


                Type       type1 = keyTypes[0];
                Type       type2 = keyTypes[1];
                MethodInfo add   = type.GetMethod("Add", new Type[2] {
                    type1, type2
                });
                MethodInfo m = orgList.GetType().GetMethod("Add");
                Dictionary <object, JsonData> dict = jd.GetObjDict();
                if (dict != null && dict.Count > 0)
                {
                    foreach (KeyValuePair <object, JsonData> item in dict)
                    {
                        object lo      = GetT(type2, item.Value);
                        Type   keyType = item.Key.GetType();
                        object keyObj;
                        if (keyType != type1)
                        {
                            if (type1 == typeof(int))
                            {
                                keyObj = int.Parse(item.Key.ToString());
                            }
                            else if (type1 == typeof(float))
                            {
                                keyObj = float.Parse(item.Key.ToString());
                            }
                            else if (type1 == typeof(double))
                            {
                                keyObj = double.Parse(item.Key.ToString());
                            }
                            else if (type1 == typeof(string))
                            {
                                keyObj = item.Key.ToString();
                            }
                            else
                            {
                                keyObj = item.Key.ToString();
                            }
                        }
                        else
                        {
                            keyObj = item.Key;
                        }
                        if (lo != null)
                        {
                            m.Invoke(orgList, new object[] { keyObj, lo });
                        }
                    }
                    return(orgList);
                }
                else
                {
                    return(null);
                }
            }

            return(null);
        }
        private string GetField(object obj)
        {
            string str      = "";
            Type   type     = obj.GetType();
            string dataType = JsonTools.GetDataType(type);

            if (dataType == DataType.NUMBER)
            {
                str = obj.ToString();
            }
            else if (dataType == DataType.STRING)
            {
                str += "\"";
                str += obj.ToString();
                str += "\"";
            }
            else if (dataType == DataType.BOOL)
            {
                str = obj.ToString().ToLower();
            }
            else if (dataType == DataType.DICTIONARY)
            {
                str = GetDictStr(obj);
            }
            else if (dataType == DataType.ARRAY)
            {
                str = GetArrayString(obj);
            }
            else if (dataType == DataType.OBJECT)
            {
                str += "{";

                FieldInfo[] fields = type.GetFields();
                for (int i = 0; i < fields.Length; i++)
                {
                    if (!fields[i].IsPublic)
                    {
                        continue;
                    }
                    if (fields[i].IsStatic)
                    {
                        continue;
                    }
                    str += "\"";
                    str += fields[i].Name;
                    str += "\":";
                    object value = fields[i].GetValue(obj);
                    if (value != null)
                    {
                        str += GetField(value);
                    }
                    else
                    {
                        str += "null";
                    }
                    if (i < fields.Length - 1)
                    {
                        str += ",";
                    }
                }
                str += "}";
            }
            else
            {
                FieldInfo[] fields = type.GetFields();
                if (fields != null && fields.Length > 0)
                {
                    str += "{";
                    for (int i = 0; i < fields.Length; i++)
                    {
                        str += "\"";
                        str += fields[i].Name;
                        str += "\":";
                        object value = fields[i].GetValue(obj);
                        if (value != null)
                        {
                            str += GetField(value);
                        }
                        else
                        {
                            str += "null";
                        }
                        if (i < fields.Length - 1)
                        {
                            str += ",";
                        }
                    }
                    str += "}";
                }
            }
            return(str);
        }
        static public object GetObjT(Type type, JsonData jd)
        {
            FieldInfo[] fields = type.GetFields();
            if (fields.Length == 0)
            {
                return(null);
            }
            object objT = (object)Activator.CreateInstance(type);

            for (int i = 0; i < fields.Length; i++)
            {
                if (!fields[i].IsPublic)
                {
                    continue;
                }
                if (fields[i].IsStatic)
                {
                    continue;
                }
                string   fieldName = fields[i].Name;
                JsonData subJd     = jd.Get(fieldName);
                if (subJd == null)
                {
                    continue;
                }
                Type   subType  = fields[i].FieldType;
                string realType = JsonTools.GetDataType(subType);
                if (realType == DataType.OBJECT)
                {
                    object subObj = GetT(subType, subJd);
                    fields[i].SetValue(objT, subObj);
                }
                else if (realType == DataType.ARRAY)
                {
                    if (subType.IsGenericType)
                    {
                        Type   listType = subType.GetMethod("Find").ReturnType;
                        object orgList  = Activator.CreateInstance(subType);

                        for (int j = 0; j < subJd.Count; j++)
                        {
                            object lo = GetT(listType, subJd.Get(j));
                            if (lo != null)
                            {
                                MethodInfo m = orgList.GetType().GetMethod("Add");
                                m.Invoke(orgList, new object[] { lo });
                            }
                        }

                        fields[i].SetValue(objT, orgList);
                    }
                    else if (subType.BaseType == typeof(Array))
                    {
                        MethodInfo[] ms       = subType.GetMethods();
                        Type         listType = subType.GetElementType();
                        object       orgList  = Array.CreateInstance(listType, subJd.Count);
                        MethodInfo   sv       = subType.GetMethod("SetValue", new Type[2] {
                            typeof(object), typeof(int)
                        });
                        for (int j = 0; j < subJd.Count; j++)
                        {
                            object lo = GetT(listType, subJd.Get(j));
                            if (lo != null)
                            {
                                sv.Invoke(orgList, new object[] { lo, j });
                            }
                        }

                        fields[i].SetValue(objT, orgList);
                    }
                }
                else if (realType == DataType.BOOL)
                {
                    bool bv = subJd.ToString().ToLower() == "true" ? true : false;
                    fields[i].SetValue(objT, bv);
                }
                else if (realType == DataType.STRING)
                {
                    fields[i].SetValue(objT, subJd.ToString());
                }
                else if (realType == DataType.NUMBER)
                {
                    string sv = subJd.ToString();
                    if (subType == typeof(int))
                    {
                        fields[i].SetValue(objT, int.Parse(sv));
                    }
                    else if (subType == typeof(float))
                    {
                        double dsv = Convert.ToDouble(sv);
                        float  fsv = Convert.ToSingle(dsv);
                        fields[i].SetValue(objT, fsv);
                    }
                    else if (subType == typeof(double))
                    {
                        fields[i].SetValue(objT, double.Parse(sv));
                    }
                    else if (subType == typeof(Single))
                    {
                        fields[i].SetValue(objT, Single.Parse(sv));
                    }
                    else if (subType == typeof(long))
                    {
                        fields[i].SetValue(objT, long.Parse(sv));
                    }
                }
                else if (realType == DataType.DICTIONARY)
                {
                    //Type listType = subType.GetMethod("Find").ReturnType;
                    object orgList = Activator.CreateInstance(subType);
                    string str     = subType.FullName;
                    string regex   = @"(?<=Dictionary`2\[).+(?=\])";
                    string match   = StringTools.GetFirstMatch(str, regex);
                    regex = @"\[[^\[\]]*(((?'Open'\[)[^\[\]]*)+((?'-Open'\])[^\[\]]*)+)*(?(Open)(?!))\]";
                    string[]    matchs   = StringTools.GetAllMatchs(match, regex);
                    List <Type> keyTypes = new List <Type>();
                    for (int j = 0; j < matchs.Length; j++)
                    {
                        string content = matchs[j].Substring(1, matchs[j].Length - 2);
                        keyTypes.Add(Type.GetType(content, true, true));
                    }


                    Type       type1 = keyTypes[0];
                    Type       type2 = keyTypes[1];
                    MethodInfo add   = subType.GetMethod("Add", new Type[2] {
                        type1, type2
                    });
                    MethodInfo m = orgList.GetType().GetMethod("Add");
                    Dictionary <object, JsonData> dict = subJd.GetObjDict();
                    if (dict != null && dict.Count > 0)
                    {
                        foreach (KeyValuePair <object, JsonData> item in dict)
                        {
                            object lo      = GetT(type2, item.Value);
                            Type   keyType = item.Key.GetType();
                            object keyObj;
                            if (keyType != type1)
                            {
                                if (type1 == typeof(int))
                                {
                                    keyObj = int.Parse(item.Key.ToString());
                                }
                                else if (type1 == typeof(float))
                                {
                                    keyObj = float.Parse(item.Key.ToString());
                                }
                                else if (type1 == typeof(double))
                                {
                                    keyObj = double.Parse(item.Key.ToString());
                                }
                                else if (type1 == typeof(string))
                                {
                                    keyObj = item.Key.ToString();
                                }
                                else
                                {
                                    keyObj = item.Key.ToString();
                                }
                            }
                            else
                            {
                                keyObj = item.Key;
                            }
                            if (lo != null)
                            {
                                m.Invoke(orgList, new object[] { keyObj, lo });
                            }
                        }
                        fields[i].SetValue(objT, orgList);
                    }
                    //for (int j = 0; j < subJd.Count; j++)
                    //{
                    //    object lo = GetT(listType, subJd.Get(j));
                    //    if (lo != null)
                    //    {
                    //        MethodInfo m = orgList.GetType().GetMethod("Add");
                    //        m.Invoke(orgList, new object[] { lo });
                    //    }
                    //}

                    //fields[i].SetValue(objT, orgList);
                }
            }
            return(objT);
        }
Exemple #4
0
        static public object GetT(Type type, JsonData jd)
        {
            string realType = JsonTools.GetDataType(type);

            if (realType == DataType.OBJECT)
            {
                return(GetObjT(type, jd));
            }
            else if (realType == DataType.ARRAY)
            {
                if (type.IsGenericType)
                {
                    Type   listType = type.GetMethod("Find").ReturnType;
                    object orgList  = Activator.CreateInstance(type);

                    for (int j = 0; j < jd.Count; j++)
                    {
                        object lo = GetT(listType, jd.Get(j));
                        if (lo != null)
                        {
                            MethodInfo m = orgList.GetType().GetMethod("Add");
                            m.Invoke(orgList, new object[] { lo });
                        }
                    }

                    return(orgList);
                }
                else if (type.BaseType == typeof(Array))
                {
                    MethodInfo[] ms       = type.GetMethods();
                    Type         listType = type.GetElementType();
                    object       orgList  = Array.CreateInstance(listType, jd.Count);
                    MethodInfo   sv       = type.GetMethod("SetValue", new Type[2] {
                        typeof(object), typeof(int)
                    });
                    for (int j = 0; j < jd.Count; j++)
                    {
                        object lo = GetT(listType, jd.Get(j));
                        if (lo != null)
                        {
                            sv.Invoke(orgList, new object[] { lo, j });
                        }
                    }

                    return(orgList);
                }
            }
            else if (realType == DataType.BOOL)
            {
                bool bv = jd.ToString().ToLower() == "true" ? true : false;
                return(bv);
            }
            else if (realType == DataType.STRING)
            {
                return(jd.ToString());
            }
            else if (realType == DataType.NUMBER)
            {
                string sv = jd.ToString();
                if (type == typeof(int))
                {
                    return(int.Parse(sv));
                }
                else if (type == typeof(float))
                {
                    double dsv = Convert.ToDouble(sv);
                    float  fsv = Convert.ToSingle(dsv);
                    return(fsv);
                }
                else if (type == typeof(double))
                {
                    return(double.Parse(sv));
                }
                else if (type == typeof(Single))
                {
                    return(Single.Parse(sv));
                }
                else if (type == typeof(long))
                {
                    return(long.Parse(sv));
                }
            }

            return(null);
        }
Exemple #5
0
        static public object GetObjT(Type type, JsonData jd)
        {
            FieldInfo[] fields = type.GetFields();
            if (fields.Length == 0)
            {
                return(null);
            }
            object objT = (object)Activator.CreateInstance(type);

            for (int i = 0; i < fields.Length; i++)
            {
                if (!fields[i].IsPublic)
                {
                    continue;
                }
                if (fields[i].IsStatic)
                {
                    continue;
                }
                string   fieldName = fields[i].Name;
                JsonData subJd     = jd.Get(fieldName);
                if (subJd == null)
                {
                    continue;
                }
                Type   subType  = fields[i].FieldType;
                string realType = JsonTools.GetDataType(subType);
                if (realType == DataType.OBJECT)
                {
                    object subObj = GetT(subType, subJd);
                    fields[i].SetValue(objT, subObj);
                }
                else if (realType == DataType.ARRAY)
                {
                    if (subType.IsGenericType)
                    {
                        Type   listType = subType.GetMethod("Find").ReturnType;
                        object orgList  = Activator.CreateInstance(subType);

                        for (int j = 0; j < subJd.Count; j++)
                        {
                            object lo = GetT(listType, subJd.Get(j));
                            if (lo != null)
                            {
                                MethodInfo m = orgList.GetType().GetMethod("Add");
                                m.Invoke(orgList, new object[] { lo });
                            }
                        }

                        fields[i].SetValue(objT, orgList);
                    }
                    else if (subType.BaseType == typeof(Array))
                    {
                        MethodInfo[] ms       = subType.GetMethods();
                        Type         listType = subType.GetElementType();
                        object       orgList  = Array.CreateInstance(listType, subJd.Count);
                        MethodInfo   sv       = subType.GetMethod("SetValue", new Type[2] {
                            typeof(object), typeof(int)
                        });
                        for (int j = 0; j < subJd.Count; j++)
                        {
                            object lo = GetT(listType, subJd.Get(j));
                            if (lo != null)
                            {
                                sv.Invoke(orgList, new object[] { lo, j });
                            }
                        }

                        fields[i].SetValue(objT, orgList);
                    }
                }
                else if (realType == DataType.BOOL)
                {
                    bool bv = subJd.ToString().ToLower() == "true" ? true : false;
                    fields[i].SetValue(objT, bv);
                }
                else if (realType == DataType.STRING)
                {
                    fields[i].SetValue(objT, subJd.ToString());
                }
                else if (realType == DataType.NUMBER)
                {
                    string sv = subJd.ToString();
                    if (subType == typeof(int))
                    {
                        fields[i].SetValue(objT, int.Parse(sv));
                    }
                    else if (subType == typeof(float))
                    {
                        double dsv = Convert.ToDouble(sv);
                        float  fsv = Convert.ToSingle(dsv);
                        fields[i].SetValue(objT, fsv);
                    }
                    else if (subType == typeof(double))
                    {
                        fields[i].SetValue(objT, double.Parse(sv));
                    }
                    else if (subType == typeof(Single))
                    {
                        fields[i].SetValue(objT, Single.Parse(sv));
                    }
                    else if (subType == typeof(long))
                    {
                        fields[i].SetValue(objT, long.Parse(sv));
                    }
                }
            }
            return(objT);
        }
Exemple #6
0
        /// <summary>
        /// 判断传入内容的类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private string GetTypeByObj(object obj)
        {
            Type type = obj.GetType();

            return(JsonTools.GetDataType(type));
        }