UnitTests/BinarySerialisationVersioningTests.cs
/// <summary>
/// If deserialising data where an older version of the type was serialised and the new version that is being deserialised to has a field that the old one did not have then
/// throw an exception. For a type to be deserialised and for some of its fields not to be set could result in confusing errors in some cases - for example, if the type has
/// a constructor that sets all fields to non-null values then consumers of that class could reasonably expect to never have to deal with a null value on any of that type's
/// properties but if the deserialisation process is allowed to leave some of those fields null then those expectations will not be met and null reference exceptions could
/// </summary>
If deserialising data where an older version of the type was serialised and the new version that is being deserialised to has a field that the old one did not have then
throw an exception. For a type to be deserialised and for some of its fields not to be set could result in confusing errors in some cases - for example, if the type has
a constructor that sets all fields to non-null values then consumers of that class could reasonably expect to never have to deal with a null value on any of that type's
properties but if the deserialisation process is allowed to leave some of those fields null then those expectations will not be met and null reference exceptions could
If deserialising data where an older version of the type was serialised and the new version that is being deserialised to has a field that the old one did not have then throw an exception . For a type to be deserialised and for some of its fields not to be set could result in confusing errors in some cases - for example if the type has a constructor that sets all fields to non - null values then consumers of that class could reasonably expect to never have to deal with a null value on any of that type ' s properties but if the deserialisation process is allowed to leave some of those fields null then those expectations will not be met and null reference exceptions could
[Fact]
		public static void IfFieldCanNotBeSetDuringDeserialisationThenThrow()
		{
			var sourceType = ConstructType(GetModuleBuilder("DynamicAssemblyFor" + GetMyName(), new Version(1, 0)), "MyClass", new Tuple<string, Type>[0]);
			var instance = Activator.CreateInstance(sourceType);
			var serialisedData = BinarySerialisation.Serialise(instance);
			var destinationType = ConstructType(
				GetModuleBuilder("DynamicAssemblyFor" + GetMyName(), new Version(1, 0)),
				"MyClass",
				new[] { Tuple.Create("Id", typeof(int)) }
			);
			Assert.Throws<FieldNotPresentInSerialisedDataException>(() =>
				ResolveDynamicAssembliesWhilePerformingAction(
					() => Deserialise(serialisedData, destinationType),
					destinationType
				)
			);
		}
[ Fact ] public static void IfFieldCanNotBeSetDuringDeserialisationThenThrow ( ) { var sourceType = ConstructType ( GetModuleBuilder ( " " + GetMyName ( ) , new Version ( 1 , 0 ) ) , " " , new Tuple < string , Type > [ 0 ] ) ; var instance = Activator . CreateInstance ( sourceType ) ; var serialisedData = BinarySerialisation . Serialise ( instance ) ; var destinationType = ConstructType ( GetModuleBuilder ( " " + GetMyName ( ) , new Version ( 1 , 0 ) ) , " " , new [ ] { Tuple . Create ( " " , typeof ( int ) ) } ) ; Assert . Throws < FieldNotPresentInSerialisedDataException > ( ( ) => ResolveDynamicAssembliesWhilePerformingAction ( ( ) => Deserialise ( serialisedData , destinationType ) , destinationType ) ) ; }
If deserialising data where an older version of the type was serialised and the new version that is being deserialised to has a field that the old one did not have then
throw an exception.
If deserialising data where an older version of the type was serialised and the new version that is being deserialised to has a field that the old one did not have then throw an exception .