简体   繁体   English

Automapper enum to Enumeration Class

[英]Automapper enum to Enumeration Class

I'm trying to use Automapper to map from a regular enum to an Enumeration Class (as described by Jimmy Bogard - http://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/ ). 我正在尝试使用Automapper从常规枚举映射到枚举类(如Jimmy Bogard所述 - http://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/ )。 The regular enum doesn't have the same values as the enumeration class does. 常规枚举与枚举类的值不同。 I would therefore like to map using the Name if possible: 因此,如果可能,我想使用名称进行映射:

Enum: 枚举:

public enum ProductType
{
    ProductType1,
    ProductType2
}

Enumeration Class: 枚举类:

public class ProductType : Enumeration
{
    public static ProductType ProductType1 = new ProductType(8, "Product Type 1");
    public static ProductType ProductType2 = new ProductType(72, "Product Type 2");

    public ProductType(int value, string displayName)
        : base(value, displayName)
    {
    }

    public ProductType()
    {
    }
}

Any help to make this mapping work appreciated! 任何帮助使这个映射工作赞赏! I have attempted just a regular mapping: 我尝试过一个常规映射:

Mapper.Map<ProductType, Domain.ProductType>();

.. but the mapped type has a value of 0. ..但映射类型的值为0。

Thanks, Alex 谢谢,亚历克斯

Here is how Automapper works - it gets public instance properties/fields of destination type, and matches the with public instance properties/fields of source type. 以下是Automapper的工作原理 - 它获取目标类型的公共实例属性/字段,并匹配源类型的公共实例属性/字段。 Your enum does not have public properties. 你的枚举没有公共属性。 Enumeration class has two - Value and DisplayName. 枚举类有两个 - Value和DisplayName。 There is nothing to map for Automapper. 没有任何地方可以映射Automapper。 Best thing you can use is simple mapper function (I like to use extension methods for that): 你可以使用的最好的东西是简单的映射器函数(我喜欢使用扩展方法):

public static Domain.ProductType ToDomainProductType(
    this ProductType productType)
{
    switch (productType)
    {
        case ProductType.ProductType1:
            return Domain.ProductType.ProductType1;
        case ProductType.ProductType2:
            return Domain.ProductType.ProductType2;
        default:
            throw new ArgumentException();
    }
}

Usage: 用法:

ProductType productType = ProductType.ProductType1;
var result = productType.ToDomainProductType();

If you really want to use Automapper in this case, you ca provide this creation method to ConstructUsing method of mapping expression: 如果您真的想在这种情况下使用Automapper,那么您可以将此创建方法提供给ConstructUsing映射表达式的方法:

Mapper.CreateMap<ProductType, Domain.ProductType>()
      .ConstructUsing(Extensions.ToDomainProductType);

You also can move this creation method to Domain.ProductType class. 您还可以将此创建方法移动到Domain.ProductType类。 Then creating its instance from given enum value will look like: 然后从给定的枚举值创建其实例将如下所示:

var result = Domain.ProductType.Create(productType);

UPDATE: You can use reflection to create generic method which maps between enums and appropriate enumeration class: 更新:您可以使用反射创建通用方法,该方法在枚举和适当的枚举类之间进行映射:

public static TEnumeration ToEnumeration<TEnum, TEnumeration>(this TEnum value)
{
    string name = Enum.GetName(typeof(TEnum), value);
    var field = typeof(TEnumeration).GetField(name);
    return (TEnumeration)field.GetValue(null);
}

Usage: 用法:

var result = productType.ToEnumeration<ProductType, Domain.ProductType>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM