static object StringToObjectForNotArray(string thing) { string[] vs = StringTool.SplitWithFormat(thing, ':'); string typ = vs[0].RemoveString(" ", "\n", "\r", "\t", "[", "]"); string typenames = TypeFormat.ToTrueTypeName(typ); int found = thing.IndexOf(':'); string a = thing.Substring(found + 1); switch (typ) { case "char": { a = StringTool.Unescape(a.TakeString('\'', '\'')[0]); break; } case "string": { a = StringTool.Unescape(a.TakeString('\"', '\"')[0]); break; } case "bool": { a = a.Replace(" ", ""); break; } } System.Reflection.MethodInfo method = typeof(Convert).GetMethod("To" + TypeFormat.TypeNameToType(typenames).Name, new Type[] { typeof(string) }); object[] data = new object[] { a }; object get = method.Invoke(null, data); if (typ == "byte") { get = (byte)get; } return(get); }
/// <summary> /// SerializationData Text to object /// </summary> /// <param name="thing">SerializationData Text</param> /// <returns>object</returns> static object ChuonStringDeserializeToObject(string thing) { string[] vs = StringTool.SplitWithFormat(thing, ':'); string typ = TypeFormat.ToSimpleTypeName(vs[0].RemoveString(" ", "\n", "\r", "\t")); object get; if (typ.Contains("[]")) { get = StringToObjectForArray(thing); } else if (typ == "Dictionary") { int found = thing.IndexOf(':'); string _data = thing.Substring(found + 1).TakeString('{', '}')[0]; int data_index = _data.IndexOf(':'); int data_index2 = _data.IndexOf(':', data_index + 1); string[] data = new string[] { _data.Substring(0, data_index), _data.Substring(data_index + 1, data_index2 - data_index - 1), _data.Substring(data_index2 + 1) }; data[0] = data[0].RemoveString(" ", "\n", "\r", "\t"); data[1] = data[1].RemoveString(" ", "\n", "\r", "\t"); string[] typenames = new string[] { TypeFormat.typelist2[Array.IndexOf(TypeFormat.typelist, data[0])], TypeFormat.typelist2[Array.IndexOf(TypeFormat.typelist, data[1])] }; Type[] types = new Type[] { TypeFormat.TypeNameToType(typenames[0]), TypeFormat.TypeNameToType(typenames[1]) }; Type thistype = typeof(Dictionary <,>).MakeGenericType(types); get = Activator.CreateInstance(thistype); if (data[2] != "NotThing") { System.Reflection.MethodInfo method = thistype.GetMethod("Add"); string[] a = data[2].TakeString('{', '}'); for (int i = 0; i < a.Length; i++) { string[] b = a[i].TakeString('{', '}'); for (int ii = 0; ii < b.Length; ii++) { int index = a[i].IndexOf("{" + b[ii] + "}"); a[i] = a[i].Substring(0, index) + "[" + ii + "]" + a[i].Substring(index + b[ii].Length + 2); } string[] nowdata = StringTool.SplitWithFormat(a[i], ','); for (int ii = 0; ii < nowdata.Length; ii++) { for (int iii = 0; iii < b.Length; iii++) { nowdata[ii] = nowdata[ii].Replace("[" + iii + "]", "{" + b[iii] + "}"); } } object key = ChuonStringDeserializeToObject(nowdata[0]); object value = ChuonStringDeserializeToObject(nowdata[1]); method.Invoke(get, new object[] { key, value }); } } } else if (typ == "null") { get = null; } else if (Array.IndexOf(TypeFormat.typelist, typ) != -1) { get = StringToObjectForNotArray(thing); } else { get = typ; } return(get); }
static object StringToObjectForArray(string thing) { string[] vs = StringTool.SplitWithFormat(thing, ':'); string typ = vs[0].RemoveString(" ", "\n", "\r", "\t", "[", "]"); string typenames = TypeFormat.ToTrueTypeName(typ); Type[] types = new Type[] { TypeFormat.TypeNameToType(typenames) }; int found = thing.IndexOf(':'); if (thing.Substring(found + 1) != "NotThing") { string a = thing.Substring(found + 1).TakeString('{', '}')[0]; if (typ == "byte") { a = a.Replace(" ", ""); return(StringTool.HexToBytes(a)); } else { string[] b = null; if (typ == "object") { b = a.TakeString('{', '}'); for (int i = 0; i < b.Length; i++) { int index = a.IndexOf("{" + b[i] + "}"); a = a.Substring(0, index) + "[" + i + "]" + a.Substring(index + b[i].Length + 2); } string[] bb = StringTool.SplitWithFormat(a, ','); for (int i = 0; i < bb.Length; i++) { for (int ii = 0; ii < b.Length; ii++) { bb[i] = bb[i].Replace("[" + ii + "]", "{" + b[ii] + "}"); } } b = bb; } else { b = StringTool.SplitWithFormat(a, ','); } Type thistype = typeof(List <>).MakeGenericType(types); IList c = (IList)Activator.CreateInstance(thistype); System.Reflection.MethodInfo toarray = thistype.GetMethod("ToArray"); if (typ == "object") { for (int i = 0; i < b.Length; i++) { c.Add(ChuonStringDeserializeToObject(b[i])); } } else { for (int i = 0; i < b.Length; i++) { object[] data = new object[] { b[i] }; switch (typ) { case "char": { data[0] = StringTool.Unescape(b[i].TakeString('\'', '\'')[0]); break; } case "string": { data[0] = StringTool.Unescape(b[i].TakeString('\"', '\"')[0]); break; } case "bool": { data[0] = b[i].Replace(" ", ""); break; } } System.Reflection.MethodInfo method = typeof(Convert).GetMethod("To" + types[0].Name, new Type[] { typeof(string) }); c.Add(method.Invoke(null, data)); } } return(toarray.Invoke(c, null)); } } else { return(Array.CreateInstance(types[0], 0)); } }