Ejemplo n.º 1
0
        public void TestDelegate()
        {
            var js = new JsonSerializer();
            js.JsonOptions.Indent = "";
            js.JsonOptions.FieldSeparator = "";

            var v1 = new SampleSelfDelegate { x = 77 };
            v1.OnSomething = v1.Handler1;
            var result = js.ToString(v1);
            Assert.AreEqual("{\"OnSomething\":\"Handler1\",\"x\":77}", result);

            var w1 = new SampleSelfDelegate();
            var jd = new JsonDeserializer();
            jd.FromString(w1, result);
            Assert.AreEqual(v1.x, w1.x);
            w1.OnSomething(10);
            Assert.AreEqual(87, w1.x);

            jd.FromString(w1, result.Replace("Handler1", "Handler2"));
            w1.OnSomething(10);
            Assert.AreEqual(770, w1.x);
        }
Ejemplo n.º 2
0
        public void TestDelegate()
        {
            var bs = new BinarySerializer();

            var v1 = new SampleSelfDelegate { x = 77 };
            v1.OnSomething = v1.Handler1;
            var result = bs.ToBytes(v1);
            Assert.AreEqual(
                "20 01 00 " + XS(typeof(SampleSelfDelegate)) + " 02 00 " +
                XS("OnSomething", RoughType.Record, "x", RoughType.Int) +
                " 01 00 " + XS("Handler1") + " 02 00 4D 00 00 00 00 00",
                XS(result));

            var w1 = new SampleSelfDelegate();
            var bd = new BinaryDeserializer();
            bd.FromBytes(w1, result);
            Assert.AreEqual(v1.x, w1.x);
            w1.OnSomething(10);
            Assert.AreEqual(87, w1.x);

            result[69]++; // Replace("Handler1", "Handler2")
            (new BinaryDeserializer()).FromBytes(w1, result);
            w1.OnSomething(10);
            Assert.AreEqual(770, w1.x);
        }