using
using AutoMapper;
設定
private IMapper Mapper
{
get
{
var config = new MapperConfiguration(cfg =>
{
// NULL は 空文字に変換(必要であれば)
cfg.CreateMap<string, string>().ConvertUsing(str => str ?? string.Empty);
//<変換基の型、変換先の型>
cfg.CreateMap<ItemModel, M_Item>();
});
return config.CreateMapper();
}
}
使用例(新規取得):
ItemModel itemModel = data.ItemBasicInfoModel; var registItem = this.Mapper.Map<M_Item>(itemModel);
ItemModelからM_Item取得(プロパティ名が同名のものを詰めたインスタンスを返す)
使用例(上書き):
M_Item m_Item = new M_Item(); m_Item.Bikou = "hoge"; ItemModel itemModel = data.ItemBasicInfoModel; //<変換基の型、変換先の型> this.Mapper.Map<ItemModel, M_Item>(itemModel, m_Item);
m_Itemに対してItemModelの値が上書きされる(プロパティ名が同名のもの)