private void testInheritanceTypeMapping <TFromBase, TFrom, TToBase, TTo>() where TFrom : TFromBase, new() where TTo : TToBase, new() { TFromBase from = new TFrom(); var to = this.mapper.Map <TToBase>(from); to.ShouldBeOfType <TTo>(); }
private void EnumTestInternal <TFrom, TTo>() { AdvancedConverter <TFrom, TTo> advancedConverter = AdvancedConverter <TFrom, TTo> .Default; AdvancedConverter <TTo, TFrom> backwardAdvancedConverter = AdvancedConverter <TTo, TFrom> .Default; IInstanceGenerator <TFrom> instanceGenerator = InstanceGeneratorProvider.Default.GetInstanceGenerator <TFrom>(); Assert.IsNotNull(advancedConverter); Assert.IsNotNull(backwardAdvancedConverter); Assert.IsNotNull(instanceGenerator); foreach (TFrom from in instanceGenerator.GetInstances(random, enumTestCount)) { TTo result = advancedConverter.Convert(from); TFrom backwardResult = backwardAdvancedConverter.Convert(result); Assert.AreEqual(from, backwardResult); } }
/// <summary> /// Serializes a given instance and deserializes it as a different type; /// this can be used to translate between wire-compatible objects (where /// two .NET types represent the same data), or to promote/demote a type /// through an inheritance hierarchy. /// </summary> /// <remarks>No assumption of compatibility is made between the types.</remarks> /// <typeparam name="TFrom">The type of the object being copied.</typeparam> /// <typeparam name="TTo">The type of the new object to be created.</typeparam> /// <param name="instance">The existing instance to use as a template.</param> /// <returns>A new instane of type TNewType, with the data from TOldType.</returns> public static TTo ChangeType <[DynamicallyAccessedMembers(DynamicAccess.ContractType)] TFrom, [DynamicallyAccessedMembers(DynamicAccess.ContractType)] TTo>(TFrom instance) { using var ms = new MemoryStream(); Serialize <TFrom>(ms, instance); ms.Position = 0; return(Deserialize <TTo>(ms)); }