自动映射器问题
尝试自动映射一些对象。
源对象具有名称前带有 _ 的属性,而目标对象则没有。 是否可以实现一个映射创建,即自动映射器将所有 _properties 映射到属性
对于所有源类型。
class MyMapper<TFrom, TTo>{
TTo PerformMap(TFrom fromObject){
Mapper.CreateMap<From, To>(); // ???
TTo result = Mapper.Map<From, To>(fromObject);
//result.Id.ShouldBe(value from TFrom._Id);
return result;
}
}
class From
{
public int _Id { get; set; }
public string _Name { get; set; }
}
class To
{
public int Id { get; set; }
public string Name { get; set; }
}
Trying to automap some objects.
Source objects has properties with _ before name, destination objects - have not.
Is it possible to implement ONE map creation, that automapper would map all _properties to properties
for all source types.
class MyMapper<TFrom, TTo>{
TTo PerformMap(TFrom fromObject){
Mapper.CreateMap<From, To>(); // ???
TTo result = Mapper.Map<From, To>(fromObject);
//result.Id.ShouldBe(value from TFrom._Id);
return result;
}
}
class From
{
public int _Id { get; set; }
public string _Name { get; set; }
}
class To
{
public int Id { get; set; }
public string Name { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近添加到 AutoMapper 中的一些内容可能会对您有所帮助 - 自定义命名约定。 如果您检查主干(R107),请四处寻找 INamingConvention。 现在,我有两个命名约定(PascalCase 和 lower_case_underscore),但这实际上只是找出正确的正则表达式来让您继续前进的问题:
INamingConvention.cs
目前,命名约定是全局的且仅限于配置文件范围。 由于此功能是新功能,因此除了测试之外没有任何文档。
Something I added recently to AutoMapper might help you - custom naming conventions. If you check out the trunk (R107), look around for INamingConvention. Right now, I have two naming conventions (PascalCase and lower_case_underscore), but it's really just a matter of figuring out the right RegEx to get you going:
INamingConvention.cs
Right now, naming conventions are global and profile-scoped. Since this feature is new, there isn't any documentation other than the tests.
我就是这样做的
This is how I'm doing it
为此,您可以添加自定义映射来解决此特殊情况:
For this you could add a custom mapping to solve this particular case: