当前位置: 首页>>代码示例>>C#>>正文


C# BinarySerializer.ClearClassIds方法代码示例

本文整理汇总了C#中BinarySerializer.ClearClassIds方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySerializer.ClearClassIds方法的具体用法?C# BinarySerializer.ClearClassIds怎么用?C# BinarySerializer.ClearClassIds使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BinarySerializer的用法示例。


在下文中一共展示了BinarySerializer.ClearClassIds方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestUnknownStorage

        public void TestUnknownStorage()
        {
            var bs = new BinarySerializer();
            var bd = new BinaryDeserializer();
            bd.Options.AllowUnknownFields = true;

            var data =
                "20 01 00 " + XS(typeof(SampleUnknown)) + " 03 00 " +
                XS("A", RoughType.String, "X", RoughType.Int, "Z", RoughType.Bool) +
                " 01 00 " + XS("qq") + " 02 00 02 01 00 00 03 00 01 00 00";
            var w = new SampleUnknown();
            bd.FromBytes(w, SX(data));
            Assert.AreEqual(258, w.X);
            Assert.AreEqual(2, w.Storage.Fields.Count);
            Assert.AreEqual("A", w.Storage.Fields[0].Name);
            Assert.AreEqual("qq", w.Storage.Fields[0].Value);
            Assert.AreEqual("Z", w.Storage.Fields[1].Name);
            Assert.AreEqual(true, w.Storage.Fields[1].Value);

            Assert.AreEqual(data, XS(bs.ToBytes(w)));

            bd.FromBytes(w, SX("20 01 00 00 00"));
            Assert.AreEqual(0, w.Storage.Fields.Count);

            Assert.AreEqual("20 01 00 00 00", XS(bs.ToBytes(new SampleUnknown())));

            bd.ClearClassIds();
            bs.ClearClassIds();
            bd.FromBytes(w, SX(
                "20 01 00 " + XS(typeof(SampleUnknown)) + " 02 00 " +
                XS("A", RoughType.String, "Z", RoughType.Bool) +
                " 01 00 " + XS("tt") + " 02 00 01 00 00"));
            Assert.AreEqual(2, w.Storage.Fields.Count);
            Assert.AreEqual("A", w.Storage.Fields[0].Name);
            Assert.AreEqual("tt", w.Storage.Fields[0].Value);
            Assert.AreEqual("Z", w.Storage.Fields[1].Name);
            Assert.AreEqual(true, w.Storage.Fields[1].Value);
            Assert.AreEqual(258, w.X);

            w.X = 0;
            Assert.AreEqual(
                "20 01 00 " + XS(typeof(SampleUnknown)) + " 03 00 " +
                XS("A", RoughType.String, "X", RoughType.Int, "Z", RoughType.Bool) +
                " 01 00 " + XS("tt") + " 03 00 01 00 00",
                XS(bs.ToBytes(w)));

            bs.ClearClassIds();
            bs.ToBytes(new SampleUnknown());
            XAssert.Throws<YuzuException>(() => bs.ToBytes(w), "SampleUnknown");

            bs.ClearClassIds();
            bd.ClearClassIds();
            var data2 =
                "20 01 00 " + XS(typeof(SampleUnknown)) + " 02 00 " +
                XS("A", RoughType.Record, "X", RoughType.Int) +
                " 01 00 02 00 " + XS("NewType") + " 01 00 " + XS("Fld", RoughType.SByte) +
                " 01 00 FE 00 00 02 00 14 00 00 00 00 00";
            var w2 = bd.FromBytes<SampleUnknown>(SX(data2));
            Assert.AreEqual(1, w2.Storage.Fields.Count);
            Assert.AreEqual("A", w2.Storage.Fields[0].Name);
            var u2 = (YuzuUnknown)w2.Storage.Fields[0].Value;
            Assert.AreEqual("NewType", u2.ClassTag);
            Assert.AreEqual(1, u2.Fields.Count);
            Assert.AreEqual((sbyte)-2, u2.Fields["Fld"]);
            Assert.AreEqual(20, w2.X);

            Assert.AreEqual("\n" + data2, "\n" + XS(bs.ToBytes(w2)));
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:68,代码来源:TestBinary.cs

示例2: TestSimple

        public void TestSimple()
        {
            var bs = new BinarySerializer();
            bs.Options.AllowEmptyTypes = true;
            Assert.AreEqual(
                XS(RoughType.Record) + " 01 00 " + XS(typeof(Empty)) + " 00 00 00 00",
                XS(bs.ToBytes(new Empty())));

            var v1 = new Sample1 { X = 345, Y = "test" };

            bs.ClearClassIds();
            var result = bs.ToBytes(v1);
            Assert.AreEqual(
                XS(RoughType.Record) + " 01 00 " + XS(typeof(Sample1)) + " 02 00 " +
                XS("X", RoughType.Int, "Y", RoughType.String) +
                " 01 00 59 01 00 00 02 00 " + XS("test") + " 00 00", XS(result));
            Sample1 v2 = new Sample1();

            var bd = new BinaryDeserializer();
            bd.FromBytes(v2, result);
            Assert.AreEqual(v1.X, v2.X);
            Assert.AreEqual(v1.Y, v2.Y);

            bd.FromBytes(v2, new byte[] { 0x20, 01, 00, 01, 00, 0xE7, 03, 00, 00, 00, 00 });
            Assert.AreEqual(999, v2.X);
            Assert.AreEqual(v1.Y, v2.Y);

            v1.X = int.MaxValue;
            bd.FromBytes(v2, bs.ToBytes(v1));
            Assert.AreEqual(v1.X, v2.X);

            v1.X = int.MaxValue;
            bd.FromBytes(v2, bs.ToBytes(v1));
            Assert.AreEqual(v1.X, v2.X);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:35,代码来源:TestBinary.cs


注:本文中的BinarySerializer.ClearClassIds方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。