如何停止Automapper将枚举值转换为字符串

时间:2021-08-30 19:15:37

Our code currently uses a very old version of Automapper (1.1) to the more recent 3.3. A change in automapper behaviour is causing some problems.

我们的代码目前使用非常旧版本的Automapper(1.1)到最近的3.3。自动机行为的变化导致了一些问题。

We have fields of type object which may take values that are either reference types or enum values. When the field values are enum values, then Automapper maps the value to the string representation.

我们有类型对象的字段,可以采用引用类型或枚举值的值。当字段值为枚举值时,Automapper将值映射到字符串表示。

See the code example below which illustrates our problem - could someone please tell me how to persuade Automapper to map the enum value to the target enum value.

请参阅下面的代码示例,它说明了我们的问题 - 有人可以告诉我如何说服Automapper将枚举值映射到目标枚举值。

Thanks in advance - chris

提前谢谢 - 克里斯

    using AutoMapper;
    using AutoMapper.Mappers;
    using NUnit.Framework;

    namespace AutoMapperTest4
    {
        [TestFixture]
        public class AutomapperTest
        {
            [Test]
            public void TestAutomapperMappingFieldsOfTypeEnumObject()
            {
                // Configure 
                var configuration = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);
                var mapper = new MappingEngine(configuration);
                IMappingExpression<Source, Target> parentMapping = configuration.CreateMap<Source, Target>();
                parentMapping.ForMember(dest => dest.Value, opt => opt.MapFrom(s => ConvertValueToTargetEnumValue(s)));

                var source = new Source { Value = SourceEnumValue.Mule };

                var target = mapper.Map<Target>(source);

                Assert.That(target.Value, Is.TypeOf<TargetEnumValue>()); // Fails.  targetParent.Value is a string "Mule".
            }

            private static TargetEnumValue ConvertValueToTargetEnumValue(Source s)
            {
                return (TargetEnumValue)s.Value;
            }
        }

        public enum SourceEnumValue
        {
            Donkey,
            Mule
        }

        public enum TargetEnumValue
        {
            Donkey,
            Mule
        }

        public class Source
        {
            public object Value { get; set; }
        }

        public class Target
        {
            public object Value { get; set; }
        }
    }

1 个解决方案

#1


You can put in an explicit mapping between each enum and object, with a ConvertUsing(e => e) to tell AutoMapper not to mess with the value.

您可以在每个枚举和对象之间放置一个显式映射,使用ConvertUsing(e => e)告诉AutoMapper不要弄乱该值。

This works, but it's horrid that one has to do it, and in some situations it's difficult to find where to put that code.

这是有效的,但是人们必须这样做是可怕的,并且在某些情况下很难找到放置代码的位置。

I would be very interested to hear from anyone who can suggest a way to get back the "correct" behaviour.

我会非常有兴趣听到任何人可以建议一种方法来恢复“正确”的行为。

#1


You can put in an explicit mapping between each enum and object, with a ConvertUsing(e => e) to tell AutoMapper not to mess with the value.

您可以在每个枚举和对象之间放置一个显式映射,使用ConvertUsing(e => e)告诉AutoMapper不要弄乱该值。

This works, but it's horrid that one has to do it, and in some situations it's difficult to find where to put that code.

这是有效的,但是人们必须这样做是可怕的,并且在某些情况下很难找到放置代码的位置。

I would be very interested to hear from anyone who can suggest a way to get back the "correct" behaviour.

我会非常有兴趣听到任何人可以建议一种方法来恢复“正确”的行为。