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++) { 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 = 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); }
/// <summary> /// 去掉换行符 /// </summary> /// <param name="str"></param> /// <returns></returns> static public string RemoveReturn(string str) { return(StringTools.Replace(str, "\r\n|\r|\n", "")); }