UnitTests/BinarySerialisationVersioningTests.cs
/// <summary>
/// For when serialising and deserialising an object to / from different versions of an assembly, if the serialised instance was newer and has more properties (and these
/// properties don't exist on the deserialisation type) then ignore them
/// </summary>
For when serialising and deserialising an object to / from different versions of an assembly, if the serialised instance was newer and has more properties (and these
properties don't exist on the deserialisation type) then ignore them
For when serialising and deserialising an object to / from different versions of an assembly if the serialised instance was newer and has more properties ( and these properties don ' t exist on the deserialisation type ) then ignore them
[Fact]
		public static void IgnoreFieldsInDataNotPresentOnDeserialisationType()
		{
			const string idFieldName = "Id";
			const string nameFieldName = "Name";
			var sourceType = ConstructType(
				GetModuleBuilder("DynamicAssemblyFor" + GetMyName(), new Version(1, 0)),
				"ClassWithIntId",
				new[]
				{
					Tuple.Create(idFieldName, typeof(int)),
					Tuple.Create(nameFieldName, typeof(string))
				}
			);
			var instance = Activator.CreateInstance(sourceType);
			var idFieldOnSource = sourceType.GetField(idFieldName);
			idFieldOnSource.SetValue(instance, 123);
			var serialisedData = BinarySerialisation.Serialise(instance);
			var destinationType = ConstructType(GetModuleBuilder("DynamicAssemblyFor" + GetMyName(), new Version(1, 0)), "ClassWithIntId", new[] { Tuple.Create(idFieldName, typeof(int)) });
			var clone = ResolveDynamicAssembliesWhilePerformingAction(
				() => Deserialise(serialisedData, destinationType),
				destinationType
			);
			var idFieldOnDestination = destinationType.GetField(idFieldName);
			Assert.Equal(123, idFieldOnDestination.GetValue(clone));
		}
[ Fact ] public static void IgnoreFieldsInDataNotPresentOnDeserialisationType ( ) { const string idFieldName = " " ; const string nameFieldName = " " ; var sourceType = ConstructType ( GetModuleBuilder ( " " + GetMyName ( ) , new Version ( 1 , 0 ) ) , " " , new [ ] { Tuple . Create ( idFieldName , typeof ( int ) ) , Tuple . Create ( nameFieldName , typeof ( string ) ) } ) ; var instance = Activator . CreateInstance ( sourceType ) ; var idFieldOnSource = sourceType . GetField ( idFieldName ) ; idFieldOnSource . SetValue ( instance , 123 ) ; var serialisedData = BinarySerialisation . Serialise ( instance ) ; var destinationType = ConstructType ( GetModuleBuilder ( " " + GetMyName ( ) , new Version ( 1 , 0 ) ) , " " , new [ ] { Tuple . Create ( idFieldName , typeof ( int ) ) } ) ; var clone = ResolveDynamicAssembliesWhilePerformingAction ( ( ) => Deserialise ( serialisedData , destinationType ) , destinationType ) ; var idFieldOnDestination = destinationType . GetField ( idFieldName ) ; Assert . Equal ( 123 , idFieldOnDestination . GetValue ( clone ) ) ; }
For when serialising and deserialising an object to / from different versions of an assembly, if the serialised instance was newer and has more properties (and these
properties don't exist on the deserialisation type) then ignore them
For when serialising and deserialising an object to / from different versions of an assembly if the serialised instance was newer and has more properties ( and these properties don ' t exist on the deserialisation type ) then ignore them