【发布时间】:2020-01-27 09:23:35
【问题描述】:
我在使用自动映射器将长值转换为枚举时遇到问题。如果我什么都不做,我会得到异常
缺少类型映射配置或不支持的映射。
映射类型: Int64 -> SomeEnum
所以如果我添加映射配置它就可以工作
public enum B
{
F1,
F2
}
public class A
{
public B FieldB { get; set; }
}
class Program
{
static void Main(string[] args)
{
var autoMapper = new Mapper(new MapperConfiguration(expression =>
{
expression.CreateMap<long, B>()
.ConvertUsing(l => (B)l);
}));
var dictionary = new Dictionary<string, object>()
{
{"FieldB", 1L}
};
var result = autoMapper.Map<A>(dictionary);
}
}
但是我必须为解决方案中的每个枚举定义它,有没有办法定义将 long 转换为 automapper 中的枚举的一般规则?
【问题讨论】:
-
对所有涉及的枚举尝试
enum : long。
标签: c# enums automapper