ExpressMapper - display / clone enumerations - InvalidCastException

I am currently facing performance issues in a solution using EmitMapper. I would like to change the implementation of my IMapper to ExpressMapper due to performance and the fact that it is actively supported.

My problem boils down to displaying Enums and the fact that enumerations should not be done directly, as this throws an InvalidCastException.

The solution is very general and dynamic in nature, and my main implementation of ExpressMapper contains the following method:

public TDestination Map<TSource, TDestination>(TSource src)
{
    return src.Map<TSource, TDestination>();
}

Having 2 identical Customer classes in different namespaces that look like this:

public class Customer
{
    public string Name { get; set; }

    public CustomerType Type { get; set; }
}

public enum CustomerType
{
    Company,
    Private,
    Government
}

I created a simple MSTest:

[TestClass]
public class ExpressMapperTest
{
    [TestMethod]
    public void SimpleMap()
    {
        // Arrange
        var myMapper = new MyMapper();
        Mappings.Source.Customer sourceCustomer = new Mappings.Source.Customer {
            Name = "John Doe",
            Type = Mappings.Source.CustomerType.Company
        };

        // Act
        var targetCustomer = myMapper.Map<Mappings.Source.Customer, Mappings.Target.Customer>(sourceCustomer);

        // Assert
        Assert.AreEqual(sourceCustomer.Name, targetCustomer.Name);
        Assert.AreEqual((int)sourceCustomer.Type, (int)targetCustomer.Type);
    }

}

Result:

ExpressMapperTest.ExpressMapperTest.SimpleMap : System.InvalidCastException: 'Mappings.Source.CustomerType' to 'Mappings.Target.CustomerType'.

System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider) System.Enum.System.IConvertible.ToType( , IFormatProvider ) System.Convert.ChangeType( , conversionType, IFormatProvider) System.Convert.ChangeType( , conversionType) lambda_method (, , ) ExpressMapper.TypeMapperBase`2.MapTo(T src, TN dest) C:\SourceCode\ExpressMapper\ExpressMapper NET40\TypeMapperBase.cs: 243 ExpressMapper.MappingServiceProvider.MapInternal [T, TN] (T src, TN dest, Boolean dynamicTrial) C:\SourceCode\ExpressMapper\ExpressMapper NET40\MappingServiceProvider.cs: 273 ExpressMapper.MappingServiceProvider.MapInternal [T, TN] (T src, TN dest, Boolean dynamicTrial) C:\SourceCode\ExpressMapper\ExpressMapper NET40\MappingServiceProvider.cs: 299 ExpressMapper.MappingServiceProvider.Map [T, TN] (T src) C:\SourceCode\ExpressMapper\ExpressMapper NET40\MappingServiceProvider.cs: 241 ExpressMapper.Mapper.Map [T, TN] (T src) C:\SourceCode\ExpressMapper\ExpressMapper NET40\Mapper.cs: 38
ExpressMapper.Extensions.ExpressmapperExtensions.Map [T, TN] (T ) C:\SourceCode\ExpressMapper\ExpressMapper NET40\ExpressmapperExtensions.cs: 11

, CustomerType, , , , , . CustomerProxy Customer ( ).

, CustomMapper, , -. - , ExpressMapper.

, , : Mappings.Source.CustomerType → int → Mappings.Target.CustomerType

.... InvalidCast.

+4
1

Expressmapper IConvertible, . Github, , . , :

Mapper.Register<Mappings.Source.Customer, Mappings.Target.Customer>()
   .Member(dst => dst.Type, src => (Mappings.Target.CustomerType)((int)src.Type));

. !

UPDATE: Expressmapper 1.8 .

+4

Source: https://habr.com/ru/post/1618483/


All Articles