这似乎是一个非常简单的问题,所以希望这很容易。
我在 Automapper 中有一个自定义映射,它简单地将string
and转换为and 。它并没有变得更简单:bool
"Y"
"N"
true
false
Mapper.CreateMap<string, bool>().ConvertUsing(str => str.ToUpper() == "Y");
这在这个原始示例中工作正常:
public class Source
{
public string IsFoo { get; set; }
public string Bar { get; set; }
public string Quux { get; set; }
}
public class Dest
{
public bool IsFoo { get; set; }
public string Bar { get; set; }
public int Quux { get; set; }
}
// ...
Mapper.CreateMap<string, bool>().ConvertUsing(str => str.ToUpper() == "Y");
Mapper.CreateMap<Source, Dest>();
Mapper.AssertConfigurationIsValid();
Source s = new Source { IsFoo = "Y", Bar = "Hello World!", Quux = "1" };
Source s2 = new Source { IsFoo = "N", Bar = "Hello Again!", Quux = "2" };
Dest d = Mapper.Map<Source, Dest>(s);
Dest d2 = Mapper.Map<Source, Dest>(s2);
但是,假设我想Source
从 a 获取数据DataReader
:
Mapper.CreateMap<string, bool>().ConvertUsing(str => str.ToUpper() == "Y");
Mapper.CreateMap<IDataReader, Dest>();
Mapper.AssertConfigurationIsValid();
DataReader reader = GetSourceData();
List<Dest> mapped = Mapper.Map<IDataReader, List<Dest>>(reader);
对于每个Dest
人来说mapped
,IsFoo
财产都是true
. 我在这里想念什么?