允许使用 AutoMapper 或类似工具映射动态类型吗?
我已经开始在项目中使用 https://github.com/robconery/massive,我想知道是否有任何映射工具允许支持动态到静态类型映射?
我以前使用过 AutoMapper,AutoMapper 支持吗?
我知道 AutoMapper 中的 DynamicMap 函数,但我相信该函数用于运行地图而无需先创建地图。在我下面的例子中它不起作用。
dynamic curUser = users.GetSingleUser(UserID);
var retUser = Mapper.DynamicMap<UserModel>(curUser);
users.GetSingleUser(UserID); // returns a dynamic object
I've started to use https://github.com/robconery/massive for a project, I wonder if there is any mapping tool that allows support for Dynamic to static type mapping?
I've used AutoMapper previously, does AutoMapper support this?
I am aware of the DynamicMap function from AutoMapper, however I believe this function is for running maps without creating the Map first. In my example below it does not work.
dynamic curUser = users.GetSingleUser(UserID);
var retUser = Mapper.DynamicMap<UserModel>(curUser);
users.GetSingleUser(UserID); // returns a dynamic object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AutoMapper 4.2.0现在支持 动态/expando/字典映射
使用此功能,您可以将 Expando 对象映射到静态类型:
旧版本的 AutoMapper 不支持此功能(Massive 内部使用
ExpandoObject
它不提供它具有哪些属性),您是对的Mapper.DynamicMap
用于映射而不创建映射配置。实际上,如果您只想要简单的映射,那么自己编写一个映射器并不难:
AutoMapper 4.2.0 now supports Dynamic/expando/dictionary mapping
With this feature you can map to your expando objects to static types:
Old versions of AutoMapper do not support this (Massive internally uses
ExpandoObject
which doesn't provide which properties it has), and you are rightMapper.DynamicMap
is for mapping without creating mapping configuration.Actually it's not hard to write yourself a mapper if you just want simple mapping:
尝试 Slapper.AutoMapper https://github.com/randyburden/Slapper.AutoMapper< /a>
它适用于
dynamic
和Dictionary
这非常棒。下面是一个示例(取自上面的 URL),展示了它与 Dictionary 配合使用是多么容易:
Try Slapper.AutoMapper https://github.com/randyburden/Slapper.AutoMapper
It works for both
dynamic
andDictionary<A, B>
which is awesome.Here's an example (taken from the URL above) showing how easily it works with Dictionary:
假设您使用的框架返回
ExpandoObject
,您可以使用 AutoMapper 实现某种动态映射:您可以使用
ConstructUsing
方法创建任何类型的复杂映射。Assuming framework you use returns
ExpandoObject
you can achieve some sort of dynamic mapping using AutoMapper:You can create any sort of complex mapping using
ConstructUsing
methods..单个对象:
列表:
示例(第 71 行): https:/ /github.com/AutoMapper/AutoMapper/blob/master/src/UnitTests/DynamicMapping.cs
Single object:
List:
Example (line 71): https://github.com/AutoMapper/AutoMapper/blob/master/src/UnitTests/DynamicMapping.cs