コード例 #1
0
 public void RegistryType(Type type)
 {
     serializer.Initialize(type);
 }
コード例 #2
0
ファイル: Test.cs プロジェクト: git-thinh/app_auto_test_demo
        static void test_add()
        {
            //var db = DataFile.Open(typeof(USER));
            //var u1 = new USER() { PASSWORD = "******", USERNAME = "******" };
            //var u2 = new USER() { PASSWORD = "******", USERNAME = "******" };

            var db = DataFile.Open(typeof(TEST));
            var u1 = new TEST()
            {
                ID = 1, LEVEL = 10, NAME = "admin"
            };
            var u2 = new TEST()
            {
                ID = 2, LEVEL = 20, NAME = "user"
            };
            var u3 = new TEST()
            {
                ID = 3, LEVEL = 30, NAME = "free"
            };

            var o1 = db.convertToDynamicObject(u1, false);
            var o2 = db.convertToDynamicObject(u2, false);
            var o3 = db.convertToDynamicObject(u3, false);

            IList ls       = (IList)typeof(List <>).MakeGenericType(db.TypeDynamic).GetConstructor(Type.EmptyTypes).Invoke(null);
            Type  listType = ls.GetType();

            app.Core.CacheFile.Serializer _boisSerializer = new app.Core.CacheFile.Serializer();
            _boisSerializer.Initialize(listType, db.TypeDynamic, typeof(TEST), list.GetType());
            byte[] b1;
            byte[] b2;
            byte[] bs;

            //using (var ms = new MemoryStream())
            //{
            //    ProtoBuf.Serializer.Serialize(ms, o1);
            //    b1 = ms.ToArray();
            //}
            using (var ms = new MemoryStream())
            {
                _boisSerializer.Serialize(o1, db.TypeDynamic, ms);
                b1 = ms.ToArray();
            }
            //b1 = System.Binary.BinarySerializer.Serialize(db.TypeDynamic, o1);

            //using (var ms = new MemoryStream())
            //{
            //    ProtoBuf.Serializer.Serialize(ms, o2);
            //    b2 = ms.ToArray();
            //}
            using (var ms = new MemoryStream())
            {
                _boisSerializer.Serialize(o2, db.TypeDynamic, ms);
                b2 = ms.ToArray();
            }
            //b2 = System.Binary.BinarySerializer.Serialize(db.TypeDynamic, o2);

            ls.Add(o1);
            ls.Add(o2);
            ls.Add(o3);
            //using (var ms = new MemoryStream())
            //{
            //    ProtoBuf.Serializer.Serialize(ms, ls);
            //    bs = ms.ToArray();
            //}

            ls.Clear();
            for (int k = 1; k < 2; k++)
            {
                //list.Add(new TEST() { ID = k, LEVEL = k * 10, NAME = k.ToString() + "-" + Guid.NewGuid().ToString() });
                ls.Add(db.convertToDynamicObject(new TEST()
                {
                    ID = k, LEVEL = k * 10, NAME = k % 9 == 0 ? null : k.ToString() + "-" + Guid.NewGuid().ToString()
                }, false));
            }

            using (var ms = new MemoryStream())
            {
                _boisSerializer.Serialize(ls, listType, ms);
                bs = ms.ToArray();
            }

            string j1 = string.Join(" ", b1.Select(x => x.ToString()).ToArray());
            string j2 = string.Join(" ", b2.Select(x => x.ToString()).ToArray());
            string js = string.Join(" ", bs.Select(x => x.ToString()).ToArray());

            byte[] bc;
            using (var ms = new MemoryStream())
            {
                //63//126//189
                _boisSerializer.Serialize(100000000, typeof(int), ms);
                bc = ms.ToArray();
            }

            string s = j1 + Environment.NewLine + j2 + Environment.NewLine + js;

            Console.WriteLine(s);


            //////////object v1 = _boisSerializer.Deserialize(b1, db.TypeDynamic, 0, b1.Length);
            //////////object v2 = _boisSerializer.Deserialize(b2, db.TypeDynamic, 0, b2.Length);
            //////////object vs = _boisSerializer.Deserialize(bs, listType, 0, bs.Length);

            byte[] bc11;
            using (var ms = new MemoryStream())
            {
                //63//126//189
                _boisSerializer.Serialize(u1, typeof(USER), ms);
                bc11 = ms.ToArray();
            }

            int iii = 0;

            //////////int mx = 1000000;
            //////////for (int k = 1; k < mx; k++)
            //////////    //list.Add(new TEST() { ID = k, LEVEL = k * 10, NAME = k.ToString() + "-" + Guid.NewGuid().ToString() });
            //////////    ls.Add(db.convertToDynamicObject(new TEST() { ID = k, LEVEL = k * 10, NAME = k % 9 == 0 ? null : k.ToString() + "-" + Guid.NewGuid().ToString() }));

            //////////string count = ls.Count.ToString();
            //////////Console.WriteLine("BEGIN TEST ...............");

            //////////byte[] bg; TimeSpan ts; string elapsedTime;
            //////////Stopwatch stopWatch = new Stopwatch();

            //////////stopWatch.Start();
            //////////using (var ms = new MemoryStream())
            //////////{
            //////////    _boisSerializer.Serialize(ls, ls.GetType(), ms);
            //////////    bg = ms.ToArray();
            //////////}
            //////////stopWatch.Stop();
            //////////ts = stopWatch.Elapsed;
            //////////elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            //////////Console.WriteLine("BOIS Serialize: " + count.ToString() + " = " + elapsedTime + " = " + bg.Length.ToString());

            //////////stopWatch.Start();
            //////////var listB = _boisSerializer.Deserialize(bg, ls.GetType(), 0, bg.Length);
            //////////stopWatch.Stop();
            //////////ts = stopWatch.Elapsed;
            //////////elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            //////////Console.WriteLine("BOIS Deserialize: " + count.ToString() + " = " + elapsedTime + " = " + bg.Length.ToString());



            //////////stopWatch.Start();
            //////////using (var ms = new MemoryStream())
            //////////{
            //////////    ProtoBuf.Serializer.Serialize(ms, ls);
            //////////    bg = ms.ToArray();
            //////////}
            //////////stopWatch.Stop();
            //////////ts = stopWatch.Elapsed;
            //////////elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            //////////Console.WriteLine("P Serialize: " + count.ToString() + " = " + elapsedTime + " = " + bg.Length.ToString());

            //////////stopWatch.Start();
            //////////using (var ms = new MemoryStream(bg))
            //////////{
            //////////    var listP = ProtoBuf.Serializer.NonGeneric.Deserialize(ls.GetType(), ms);
            //////////}
            //////////stopWatch.Stop();
            //////////ts = stopWatch.Elapsed;
            //////////elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            //////////Console.WriteLine("P Deserialize: " + count.ToString() + " = " + elapsedTime + " = " + bg.Length.ToString());



            //////////string sss = "";
            ////////////////if (db.Opened)
            ////////////////{
            ////////////////    var ra = db.AddItems(new USER[] {
            ////////////////        new USER() { PASSWORD = "******", USERNAME = "******" },
            ////////////////        new USER() { PASSWORD = "******", USERNAME = "******" },
            ////////////////        new USER() { PASSWORD = "******", USERNAME = "******" },
            ////////////////    });
            ////////////////}

            //////////////var ab = DataFile.Open(typeof(TEST));
            //////////////if (ab.Opened)
            //////////////{
            //////////////    var r0 = ab.AddItems(new TEST[] {
            //////////////        new TEST() { ID = 1, NAME = "item 1" },
            //////////////        new TEST() { ID = 2, NAME = "item 2" },
            //////////////        new TEST() { ID = 3, NAME = "item 3" },
            //////////////    });
            //////////////}

            ////////////var r1 = ab.AddItems(new Test[] { new Test() { Id = ab.Count, Name = ab.Count.ToString() } });
            ////////////var r2 = ab.AddItems(new Test[] { new Test() { Id = 1, Name = "1" }, new Test(), new Test(), new Test() { Id = 3, Name = "3" } });
            ////////////var r3 = ab.AddItems(new Test[] { new Test() { Id = 2, Name = "item 2" } });
            ////////////var r4 = ab.AddItem(list[1]);
            ////////////var r5 = ab.AddItems(list.ToArray());
        }