オートマッパーを使用しています。私には 2 つのクラスがあります。単一のプロパティを持つ TypeA。2 つのプロパティを持つ TypeB。そのうちの 1 つはプライベート セッターを持ち、このプロパティの値はコンストラクタを介して渡されます。TypeB にはデフォルトのコンストラクターがありません。
質問: Automapper を構成して TypeA を TypeB に変換することは可能ですか?
public class TypeA
{
public string Property1 { get; set; }
}
public class TypeB
{
public TypeB(int contextId)
{ ContextId = contextId; }
public string Property1 { get; set; }
public int ContextId { get; private set; }
}
public class Context
{
private int _id;
public void SomeMethod()
{
TypeA instanceOfA = new TypeA() { Property1 = "Some string" };
// How to configure Automapper so, that it uses constructor of TypeB
// and passes "_id" field value into this constructor?
// Not work, since "contextId" must be passed to constructor of TypeB
TypeB instanceOfB = Mapper.Map<TypeB>(instanceOfA);
// Goal is to create folowing object
instanceOfB = new TypeB(_id) { Property1 = instanceOfA.Property1 };
}
}