3
好吧,我希望我只是以某種方式忽略明顯。我有下面的代碼情況。出於某種原因,即使我調用Ignore(),SequenceNo屬性仍然得到映射。我正在使用最新版本。我也在同一個項目中使用了兩個不同的類來測試它,並且它似乎可行,那麼這種情況有什麼問題呢?AutoMapper忽略()問題
這是域對象:
public class CableID
{
private string _panelID1;
public string PanelID1
{
get { return _panelID1; }
private set { _panelID1 = value; }
}
private string _panelID2;
public string PanelID2
{
get { return _panelID2; }
private set { _panelID2 = value; }
}
private int _sequenceNo;
public int SequenceNo
{
get { return _sequenceNo; }
private set { _sequenceNo = value; }
}
private DateTime _inService;
public DateTime InService
{
get { return _inService; }
set { _inService = value; }
}
private string _id;
public string ID
{
get { return _id; }
private set { _id = value; }
}
public CableID(string panelID1, string panelID2, int sequenceNo)
{
this.PanelID1 = panelID1;
this.PanelID2 = panelID2;
this.SequenceNo = sequenceNo;
this.ID = string.Format("({0}-{1}){2}", this.PanelID1, this.PanelID2, this.SequenceNo);
}
public CableID(string id)
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException("id");
this.ID = id;
}
}
這裏是DTO對象:
public class CableIDDTO
{
private string _panelID1;
public string PanelID1
{
get { return _panelID1; }
set { _panelID1 = value; }
}
private string _panelID2;
public string PanelID2
{
get { return _panelID2; }
set { _panelID2 = value; }
}
private int _sequenceNo;
public int SequenceNo
{
get { return _sequenceNo; }
set { _sequenceNo = value; }
}
private string _id;
public string ID
{
get { return _id; }
set { _id = value; }
}
public CableIDDTO()
{ }
public CableIDDTO(string panelID1, string panelID2, int sequenceNo)
{
this.PanelID2 = panelID1;
this.PanelID1 = panelID2;
this.SequenceNo = sequenceNo;
this.ID = string.Format("({0}-{1}){2}", this.PanelID2, this.PanelID1, this.SequenceNo);
}
}
最後的AutoMapper用例:
CableID cableID = new CableID("A1", "B1", 2);
Mapper.CreateMap<CableID, CableIDDTO>()
.ForMember(dest => dest.SequenceNo, opt => opt.Ignore());
CableIDDTO dto = Mapper.Map<CableID, CableIDDTO>(cableID);
dto.SequenceNo = 2時,因爲我已經設置忽略()它應該是0。
你釘它!這並不直觀,誠實也沒有多大意義。爲什麼它在無參數構造函數可用時抓取那個?如果有三個或四個重載的構造函數呢? – bjhuffine 2014-11-05 21:11:22
我不確定 - 我今天下午需要一些時間,並通過AutoMapper的源代碼來查看我是否可以找出 – 2014-11-05 21:15:36
@bjhuffine:添加了一些更多信息,希望有所幫助。直到你的問題我都不知道! – 2014-11-05 22:27:12