次のようなプロパティ名でオブジェクトをマップしようとしています:
Property_One -> PropertyOne ... etc
Sample_Property -> SampleProperty
各プロパティを別のプロパティに個別にマップするよりも、これを行うためのより良い方法はありますか? 唯一の違いは下線です。
次のようなプロパティ名でオブジェクトをマップしようとしています:
Property_One -> PropertyOne ... etc
Sample_Property -> SampleProperty
各プロパティを別のプロパティに個別にマップするよりも、これを行うためのより良い方法はありますか? 唯一の違いは下線です。
ソース側でアンダースコアの命名規則を指定する必要があります。
Mapper.Initialize(i =>
{
i.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
i.CreateMap<Source, Dest>();
});
一部のソース タイプのみがこの命名規則に従っている場合は、グローバルに (上記のように) またはプロファイルごとに行うことができます。
public class Source
{
public string Property_One { get; set; }
}
public class Dest
{
public string PropertyOne { get; set; }
}
class Program
{
static void Main(string[] args)
{
Mapper.CreateMap<Source, Dest>()
.ForMember(dest => dest.PropertyOne,
opt => opt.MapFrom(src => src.Property_One));
var source = new Source
{
Property_One = "property1"
};
var destination = Mapper.Map<Source, Dest>(source);
Console.WriteLine(destination.PropertyOne);
}
}