如何解决Automapper无法从继承自字典的自定义模型进行映射
我有一个继承自 Dictionary
在下面提供一个虚拟示例;
下面是我的模特;
public class MyModel : Dictionary<string,string> // All values in dictionary maps correctly
{
public SomeObject Prop1 { get; set; } // Won't map
public int Prop2 { get; set; } // Won't map
public string Prop3 { get; set; } // Won't map
public string Prop4 { get; set; } // Won't map
public bool Prop5 { get; set; } // Won't map
}
下面是另外两个将通过自动映射器相互映射的模型。
public class BaseObject : ICommonInterface
{
public IList<MyModel> MyList { get; set; }
public int Prop1 { get; set; }
public bool Prop2 { get; set; }
}
public class TargetObject : ICommonInterface
{
public IList<MyModel> MyList { get; set; } // MyModel properties values won't map,dictionary values map ok.
public int Prop1 { get; set; } // Maps correctly
public bool Prop2 { get; set; } // Maps correctly
}
我使用以下代码进行映射;基本模型在MyModel列表中填充了5个属性。
var model = new MyModel();
model["a"] = "some value"; // This maps ok
model["b"] = "some other value"; // This maps ok
model.Prop5 = true; // This will not be mapped in end result.
baseObject.MyList.Add(model);
_mapper.Map(baseObject,targetObject);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。