我正在尝试找到一种方法,让 Automapper 根据 Source 类型中设置的 Enum 值来选择调用映射的目标类型...
例如,给定以下类:
public class Organisation
{
public string Name {get;set;}
public List<Metric> Metrics {get;set;}
}
public class Metric
{
public int NumericValue {get;set;}
public string TextValue {get;set;}
public MetricType MetricType {get;set;}
}
public enum MetricType
{
NumericMetric,
TextMetric
}
如果我有以下对象:
var Org = new Organisation {
Name = "MyOrganisation",
Metrics = new List<Metric>{
new Metric { Type=MetricType.TextMetric, TextValue = "Very Good!" },
new Metric { Type=MetricType.NumericMetric, NumericValue = 10 }
}
}
现在,我想将其映射到具有类的视图模型表示:
public class OrganisationViewModel
{
public string Name {get;set;}
public List<IMetricViewModels> Metrics {get;set;}
}
public NumericMetric : IMetricViewModels
{
public int Value {get;set;}
}
public TextMetric : IMetricViewModels
{
public string Value {get;set;}
}
对 AutoMapper.Map 的调用将产生一个包含一个 NumericMetric 和一个 TextMetric 的 OrganisationViewModel。
Automapper 调用:
var vm = Automapper.Map<Organisation, OrganisationViewModel>(Org);
我将如何配置 Automapper 来支持这一点?这可能吗?(我希望这个问题很清楚)
谢谢!