这看起来像是一个非常简单的问题,所以希望这很容易。使用Automapper在DataReader中将字符串转换为bool
我在Automapper简单地转换"Y"
和"N"
到true
和false
定制string
到bool
地图。它没有得到更简单:
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);
但是,让我们说不是我想从DataReader
采取Source
数据:
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
in mapped
,IsFoo
财产是true
。我在这里错过了什么?
我怀疑这可能是问题的一部分,并尝试从'object'映射到'bool'而不是没有运气。我可能刚刚搞砸了,但我不想为布尔值创建太大的映射 - 如果这甚至是一个有效的关注。 –