예제 #1
0
        static object GetTyp(BinaryReader reader)
        {
            byte   data = reader.ReadByte();
            object get;

            if (data < TypeFormat.type.Length)
            {
                string typ = TypeFormat.type[data];
                if (typ.Contains("[]"))
                {
                    get = GetTypArray(typ, reader);
                }
                else if (typ == "Dictionary`2")
                {
                    string[] typenames = new string[] { TypeFormat.type[reader.ReadByte()], TypeFormat.type[reader.ReadByte()] };
                    Type[]   types     = new Type[] { TypeFormat.TypeNameToType(typenames[0]), TypeFormat.TypeNameToType(typenames[1]) };

                    Type thistype = typeof(Dictionary <,>).MakeGenericType(types);
                    System.Reflection.MethodInfo method = thistype.GetMethod("Add");

                    IDictionary d     = (IDictionary)Activator.CreateInstance(thistype);
                    int         count = GetIntLength(reader);
                    for (int ii = 0; ii < count; ii++)
                    {
                        object key   = GetTyp(reader);
                        object value = GetTyp(reader);
                        method.Invoke(d, new object[] { key, value });
                    }
                    get = d;
                }
                else if (typ == "null")
                {
                    bool a = reader.ReadBoolean();
                    get = null;
                }
                else if (Array.IndexOf(TypeFormat.type, typ) != -1)
                {
                    System.Reflection.MethodInfo method = typeof(BinaryReader).GetMethod("Read" + TypeFormat.TypeNameToType(typ).Name);
                    get = method.Invoke(reader, null);
                }
                else
                {
                    get = typ;
                }
            }
            else
            {
                get = reader.ReadString();
            }
            return(get);
        }
예제 #2
0
        static object GetTypArray(string typ, BinaryReader reader)
        {
            int count = GetIntLength(reader);

            typ = typ.RemoveString("[", "]");
            if (typ == "Byte" || typ == "Char")
            {
                System.Reflection.MethodInfo method = typeof(BinaryReader).GetMethod("Read" + TypeFormat.TypeNameToType(typ).Name + "s");
                return(method.Invoke(reader, new object[] { count }));
            }
            else
            {
                Array d = Array.CreateInstance(TypeFormat.TypeNameToType(typ), count);
                System.Reflection.MethodInfo method = typeof(BinaryReader).GetMethod("Read" + TypeFormat.TypeNameToType(typ).Name);
                for (int i = 0; i < d.Length; i++)
                {
                    if (typ == "Object")
                    {
                        d.SetValue(GetTyp(reader), i);
                    }
                    else
                    {
                        d.SetValue(method.Invoke(reader, null), i);
                    }
                }
                return(d);
            }
        }
예제 #3
0
        static void TypingNotArray(BinaryWriter writer, object thing)
        {
            string typename = thing.GetType().Name;

            writer.Write((byte)(Array.IndexOf(TypeFormat.type, typename)));
            System.Reflection.MethodInfo write = typeof(BinaryWriter).GetMethod("Write", new Type[] { TypeFormat.TypeNameToType(typename) });
            write.Invoke(writer, new object[] { thing });
        }
예제 #4
0
        static void TypingArray(BinaryWriter writer, object thing)
        {
            Array  c        = (Array)thing;
            string typename = thing.GetType().Name;

            writer.Write((byte)(Array.IndexOf(TypeFormat.type, typename)));
            writer.Write(GetBytesLength(c.Length));
            typename = typename.RemoveString("[", "]");
            if (typename == "Byte" || typename == "Char")
            {
                typename += "[]";
                System.Reflection.MethodInfo write = typeof(BinaryWriter).GetMethod("Write", new Type[] { TypeFormat.TypeNameToType(typename) });
                write.Invoke(writer, new object[] { c });
            }
            else
            {
                System.Reflection.MethodInfo write = typeof(BinaryWriter).GetMethod("Write", new Type[] { TypeFormat.TypeNameToType(typename) });
                for (int ii = 0; ii < c.Length; ii++)
                {
                    if (typename == "Object")
                    {
                        Typing(writer, c.GetValue(ii));
                    }
                    else
                    {
                        write.Invoke(writer, new object[] { c.GetValue(ii) });
                    }
                }
            }
        }
        /// <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));
            }
        }