UnitTests/BinarySerialisationVersioningTests.cs
/// <summary>
/// This is a companion to DoNotThrowWhenTryingToParseUnavailableTypeIfThereIsNoFieldThatTheTypeWouldBeUsedToSet to illustrate that it is NOT ok to ignore a type
/// that is not available if the instance of that type would be used to populate a property
/// </summary>
This is a companion to DoNotThrowWhenTryingToParseUnavailableTypeIfThereIsNoFieldThatTheTypeWouldBeUsedToSet to illustrate that it is NOT ok to ignore a type
that is not available if the instance of that type would be used to populate a property
This is a companion to DoNotThrowWhenTryingToParseUnavailableTypeIfThereIsNoFieldThatTheTypeWouldBeUsedToSet to illustrate that it is NOT ok to ignore a type that is not available if the instance of that type would be used to populate a property
[Fact]
		public static void DeserialisationWillFailIfTypeRequiredToSetFieldValueIsNotAvailable()
		{
			var valueFieldName = "Value";
			var sourceModule = GetModuleBuilder("DynamicAssemblyFor" + GetMyName(), new Version(1, 0));
			var nestedSourceType = ConstructType(sourceModule, "MyNestedClass", new Tuple<string, Type>[0]);
			var sourceType = ConstructType(sourceModule, "MyClass", new[] { Tuple.Create(valueFieldName, nestedSourceType) });
			var instance = Activator.CreateInstance(sourceType);
			sourceType.GetField(valueFieldName).SetValue(instance, Activator.CreateInstance(nestedSourceType));
			var serialisedData = BinarySerialisation.Serialise(instance);
			var destinationModule = GetModuleBuilder("DynamicAssemblyFor" + GetMyName(), new Version(1, 0));
			var destinationType = ConstructType(destinationModule, "MyClass", new[] { Tuple.Create(valueFieldName, nestedSourceType) });
			var nestedDestinationType = ConstructType(destinationModule, "MyOtherNestedClass", new Tuple<string, Type>[0]);
			Assert.Throws<TypeLoadException>(() =>
				ResolveDynamicAssembliesWhilePerformingAction(
					() => Deserialise(serialisedData, destinationType),
					destinationType,
					nestedDestinationType
				)
			);
		}
[ Fact ] public static void DeserialisationWillFailIfTypeRequiredToSetFieldValueIsNotAvailable ( ) { var valueFieldName = " " ; var sourceModule = GetModuleBuilder ( " " + GetMyName ( ) , new Version ( 1 , 0 ) ) ; var nestedSourceType = ConstructType ( sourceModule , " " , new Tuple < string , Type > [ 0 ] ) ; var sourceType = ConstructType ( sourceModule , " " , new [ ] { Tuple . Create ( valueFieldName , nestedSourceType ) } ) ; var instance = Activator . CreateInstance ( sourceType ) ; sourceType . GetField ( valueFieldName ) . SetValue ( instance , Activator . CreateInstance ( nestedSourceType ) ) ; var serialisedData = BinarySerialisation . Serialise ( instance ) ; var destinationModule = GetModuleBuilder ( " " + GetMyName ( ) , new Version ( 1 , 0 ) ) ; var destinationType = ConstructType ( destinationModule , " " , new [ ] { Tuple . Create ( valueFieldName , nestedSourceType ) } ) ; var nestedDestinationType = ConstructType ( destinationModule , " " , new Tuple < string , Type > [ 0 ] ) ; Assert . Throws < TypeLoadException > ( ( ) => ResolveDynamicAssembliesWhilePerformingAction ( ( ) => Deserialise ( serialisedData , destinationType ) , destinationType , nestedDestinationType ) ) ; }
This is a companion to DoNotThrowWhenTryingToParseUnavailableTypeIfThereIsNoFieldThatTheTypeWouldBeUsedToSet to illustrate that it is NOT ok to ignore a type
that is not available if the instance of that type would be used to populate a property
This is a companion to DoNotThrowWhenTryingToParseUnavailableTypeIfThereIsNoFieldThatTheTypeWouldBeUsedToSet to illustrate that it is NOT ok to ignore a type that is not available if the instance of that type would be used to populate a property
// The source type will have a name that matches the deserialisation type but the "Value" property will have different types on the source and destination types // and so the deserialisation attempt should fail