Automapper枚举到枚举类 [英] Automapper enum to Enumeration Class
问题描述
我试图用Automapper从一个普通枚举枚举类(如吉米·博加德描述地图 - 的 http://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/ )。作为枚举类不经常枚举不具有相同的价值观。因此,我想尽可能使用该名称映射:
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/). 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:
枚举:
public enum ProductType
{
ProductType1,
ProductType2
}
枚举类:
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>();
..但映射类型都有值为0。
.. but the mapped type has a value of 0.
谢谢,
亚历
推荐答案
下面是Automapper如何工作 - 它得到公众的例如的性能/目标类型的字段,以及与公众的实例相匹配的的性能/源类型的字段。您枚举不具有公共属性。枚举类有两个 - 值和显示名称。没有什么可以映射为Automapper。您可以使用最好的事情是简单的映射功能(我喜欢用这个扩展方法):
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. Your enum does not have public properties. Enumeration class has two - Value and DisplayName. There is nothing to map for 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();
如果你真的想在这种情况下使用Automapper,你ca的提供这种创作方法 ConstructUsing
映射的表达方式:
If you really want to use Automapper in this case, you ca provide this creation method to ConstructUsing
method of mapping expression:
Mapper.CreateMap<ProductType, Domain.ProductType>()
.ConstructUsing(Extensions.ToDomainProductType);
您还可以将这种创作方法 Domain.ProductType
类。然后创建从给定的枚举值它的实例会看起来像:
You also can move this creation method to Domain.ProductType
class. 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>();
这篇关于Automapper枚举到枚举类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!