UnitTests/BinarySerialisationVersioningTests.cs
/// <summary>
/// This relates to IfFieldCanNotBeSetDuringDeserialisationThenThrow - ordinarily, if the serialisation data can't set a value on a field then that should mean that the
/// deserialisation has failed but if that field has the [OptionalWhenDeserialising] on it then that means that it's ok
/// </summary>
This relates to IfFieldCanNotBeSetDuringDeserialisationThenThrow - ordinarily, if the serialisation data can't set a value on a field then that should mean that the
deserialisation has failed but if that field has the [OptionalWhenDeserialising] on it then that means that it's ok
This relates to IfFieldCanNotBeSetDuringDeserialisationThenThrow - ordinarily if the serialisation data can ' t set a value on a field then that should mean that the deserialisation has failed but if that field has the [ OptionalWhenDeserialising ] on it then that means that it ' s ok
[Fact]
		public static void AllowDeserialisationIfFieldCanNotBeSetIfFieldIsMarkedAsOptionalForDeserialisation()
		{
			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);
			const string nameFieldName = "Name";
			var destinationType = ConstructType(
				GetModuleBuilder("DynamicAssemblyFor" + GetMyName(), new Version(1, 0)),
				"MyClass",
				fields: new Tuple<string, Type>[0],
				optionalFinisher: typeBuilder =>
				{
					var fieldBuilder = typeBuilder.DefineField(nameFieldName, typeof(string), FieldAttributes.Public);
					fieldBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(OptionalWhenDeserialisingAttribute).GetConstructor(Type.EmptyTypes), new object[0]));
				}
			);
			var clone = ResolveDynamicAssembliesWhilePerformingAction(
				() => Deserialise(serialisedData, destinationType),
				destinationType
			);
			var nameFieldOnDestination = destinationType.GetField(nameFieldName);
			Assert.Null(nameFieldOnDestination.GetValue(clone));
		}
[ Fact ] public static void AllowDeserialisationIfFieldCanNotBeSetIfFieldIsMarkedAsOptionalForDeserialisation ( ) { var sourceType = ConstructType ( GetModuleBuilder ( " " + GetMyName ( ) , new Version ( 1 , 0 ) ) , " " , new Tuple < string , Type > [ 0 ] ) ; var instance = Activator . CreateInstance ( sourceType ) ; var serialisedData = BinarySerialisation . Serialise ( instance ) ; const string nameFieldName = " " ; var destinationType = ConstructType ( GetModuleBuilder ( " " + GetMyName ( ) , new Version ( 1 , 0 ) ) , " " , fields : new Tuple < string , Type > [ 0 ] , optionalFinisher : typeBuilder => { var fieldBuilder = typeBuilder . DefineField ( nameFieldName , typeof ( string ) , FieldAttributes . Public ) ; fieldBuilder . SetCustomAttribute ( new CustomAttributeBuilder ( typeof ( OptionalWhenDeserialisingAttribute ) . GetConstructor ( Type . EmptyTypes ) , new object [ 0 ] ) ) ; } ) ; var clone = ResolveDynamicAssembliesWhilePerformingAction ( ( ) => Deserialise ( serialisedData , destinationType ) , destinationType ) ; var nameFieldOnDestination = destinationType . GetField ( nameFieldName ) ; Assert . Null ( nameFieldOnDestination . GetValue ( clone ) ) ; }
This relates to IfFieldCanNotBeSetDuringDeserialisationThenThrow - ordinarily, if the serialisation data can't set a value on a field then that should mean that the
deserialisation has failed but if that field has the [OptionalWhenDeserialising] on it then that means that it's ok
This relates to IfFieldCanNotBeSetDuringDeserialisationThenThrow - ordinarily if the serialisation data can ' t set a value on a field then that should mean that the deserialisation has failed but if that field has the [ OptionalWhenDeserialising ] on it then that means that it ' s ok
// Need to define the field using this lambda rather than specifying it through the fields argument because we need to set the custom attribute on it