1

のすべてのプロパティをコピーsourceObjectするtargetObjectが、に記載されているメソッドはコピーしない汎用更新メソッドが必要ですexceptions

4

1 に答える 1

0

使ってみましたAutoMapperか?カスタム マッピングと自動マッピングを定義できます。

- 編集 -

例:

次のタイプを指定します。

public class Type1
{
    public int MyProperty1 { get; set; }
    public int MyProperty2 { get; set; }
    public int MyProperty3 { get; set; }
    public int MyProperty4 { get; set; }
    public int MyProperty5 { get; set; }
}

public class Type2
{
    public int MyProperty1 { get; set; }
    public int MyProperty2 { get; set; }
    public int MyProperty3 { get; set; }
    public int MyProperty7 { get; set; }
    public int MyProperty8 { get; set; }
}

2 つのプロパティ (MyProperty7 と MyProperty8) を無視するマップを作成するには:

var map = Mapper.CreateMap<Type1, Type2>().
            ForMember(dest => dest.MyProperty7, opt => opt.Ignore()).
            ForMember(dest => dest.MyProperty8, opt => opt.Ignore());

最後にコピーします:

Type2 type2Variable = Mapper.Map<Type1, Type2>(type1Variable);

--edit2--

同じタイプのマッピングの例:

var map = Mapper.CreateMap<Type1, Type1>().
            ForMember(dest => dest.MyProperty4, opt => opt.Ignore()).
            ForMember(dest => dest.MyProperty5, opt => opt.Ignore());

使用する:

Type1 anotherType1Variable = Mapper.Map<Type1, Type1>(type1Variable);
于 2010-12-08T08:45:12.253 に答える