コード例 #1
0
        public void ILSerializer_AllowCopiedFieldsToDifferFromSerializedFields()
        {
            var input = new FieldTest
            {
                One   = 1,
                Two   = 2,
                Three = 3
            };

            var generator   = new ILSerializerGenerator();
            var serializers = generator.GenerateSerializer(input.GetType(), f => f.Name != "One", f => f.Name != "Three");
            var writer      = new SerializationContext(this.fixture.SerializationManager)
            {
                StreamWriter = new BinaryTokenStreamWriter()
            };
            var copy = (FieldTest)serializers.DeepCopy(input, writer);

            Assert.Equal(1, copy.One);
            Assert.Equal(2, copy.Two);
            Assert.Equal(0, copy.Three);

            serializers.Serialize(input, writer, input.GetType());
            var reader = new DeserializationContext(this.fixture.SerializationManager)
            {
                StreamReader = new BinaryTokenStreamReader(writer.StreamWriter.ToByteArray())
            };
            var deserialized = (FieldTest)serializers.Deserialize(input.GetType(), reader);

            Assert.Equal(0, deserialized.One);
            Assert.Equal(2, deserialized.Two);
            Assert.Equal(3, deserialized.Three);
        }
コード例 #2
0
        public void ILSerialized_NonSerializedFields()
        {
            var input = new FieldTest
            {
                One = 1,
                Two = 2,
                NonSerializedInt = 1098
            };
            var generator   = new ILSerializerGenerator();
            var serializers = generator.GenerateSerializer(input.GetType());
            var writer      = new SerializationContext(this.fixture.SerializationManager)
            {
                StreamWriter = new BinaryTokenStreamWriter()
            };

            serializers.Serialize(input, writer, input.GetType());
            var reader = new DeserializationContext(this.fixture.SerializationManager)
            {
                StreamReader = new BinaryTokenStreamReader(writer.StreamWriter.ToByteArray())
            };
            var deserialized = (FieldTest)serializers.Deserialize(input.GetType(), reader);

            Assert.Equal(input.One, deserialized.One);
            Assert.Equal(input.Two, deserialized.Two);
            Assert.NotEqual(input.NonSerializedInt, deserialized.NonSerializedInt);
            Assert.Equal(default(int), deserialized.NonSerializedInt);
        }
コード例 #3
0
        public void ILSerializer_AllowCopiedFieldsToDifferFromSerializedFields()
        {
            var input = new FieldTest
            {
                One = 1,
                Two = 2,
                Three = 3
            };

            var generator = new ILSerializerGenerator();
            var serializers = generator.GenerateSerializer(input.GetType(), f => f.Name != "One", f => f.Name != "Three");
            var copy = (FieldTest)serializers.DeepCopy(input);
            Assert.Equal(1, copy.One);
            Assert.Equal(2, copy.Two);
            Assert.Equal(0, copy.Three);

            var writer = new BinaryTokenStreamWriter();
            serializers.Serialize(input, writer, input.GetType());
            var reader = new BinaryTokenStreamReader(writer.ToByteArray());
            var deserialized = (FieldTest)serializers.Deserialize(input.GetType(), reader);

            Assert.Equal(0, deserialized.One);
            Assert.Equal(2, deserialized.Two);
            Assert.Equal(3, deserialized.Three);
        }
コード例 #4
0
        public void ILSerializer_LifecycleHooksAreCalled()
        {
            var input       = new FieldTest();
            var generator   = new ILSerializerGenerator();
            var serializers = generator.GenerateSerializer(input.GetType());
            var writer      = new SerializationContext(this.fixture.SerializationManager)
            {
                StreamWriter = new BinaryTokenStreamWriter()
            };

            serializers.Serialize(input, writer, input.GetType());
            var reader = new DeserializationContext(this.fixture.SerializationManager)
            {
                StreamReader = new BinaryTokenStreamReader(writer.StreamWriter.ToByteArray())
            };
            var deserialized = (FieldTest)serializers.Deserialize(input.GetType(), reader);

            Assert.Null(input.Context);
            Assert.NotNull(deserialized.Context);
            Assert.Equal(this.fixture.SerializationManager, deserialized.Context.SerializationManager);
        }
コード例 #5
0
        public void TestEmitField()
        {
            var c        = new FieldTest(1);
            var cType    = c.GetType();
            var cMethod  = cType.GetField(nameof(c.Value));
            var expected = c.Value;
            var builder  = new ILInstructionBuilder();

            builder.Write(OpCodes.Ldarg_0);
            builder.Write(OpCodes.Ldfld, cMethod.MetadataToken);
            builder.Write(OpCodes.Ret);
            var ilMethod = new ILMethod(MethodBase.GetCurrentMethod().Name, expected.GetType());

            ilMethod.AddParameters(new[] { cType });
            ilMethod.AddInstructions(builder.Instructions.ToArray());
            ilMethod.Module = this.GetType().Module;
            var method = ilMethod.Compile();
            var actual = method.Invoke(null, new[] { c });

            Assert.IsTrue((int)actual == expected, $"Actual: {actual}\r\nExpected:{expected}");
        }
コード例 #6
0
 public static void Delete(Flags toDelete, FieldTest fieldTest)
 {
     _instance.Delete(toDelete, fieldTest);
 }
コード例 #7
0
 public static void Refresh(FieldTest fieldTest, Flags toPopulate)
 {
     _instance.Refresh(new FieldTestList(fieldTest), toPopulate);
 }
コード例 #8
0
 public static void Populate(FieldTest fieldTest, Flags toPopulate)
 {
     _instance.Populate(fieldTest, toPopulate);
 }