使用嵌套 TrackableCollection 反序列化自跟踪实体
如何将 JSON 字符串反序列化为这样的类型的实体(删除自跟踪属性以使其简单):
public class User:
{
int Id { get; set; }
string Name { get; set; }
public TrackableCollection<Role> Roles { get; set; } // <!
}
角色也是具有两个属性的简单类。 TrackableCollection 是 Collection (System.Collections.ObjectModel) 的后代。
所以我想要:有这样的 JSON 字符串
{"Id":0, "Name":"Test User", "Roles": [{"Id":1, "Name": "Role 1"}, {"Id":2, "Name": "Role 2"}, {"Id":3, "Name": "Role 3"}]}
获取具有正确反序列化 Roles 集合的实体。
How can I deserialize JSON string into entity of type like this (self-tracking properties removed to be simple):
public class User:
{
int Id { get; set; }
string Name { get; set; }
public TrackableCollection<Role> Roles { get; set; } // <!
}
Role is also simple class with two properties. TrackableCollection is descendant of Collection (System.Collections.ObjectModel).
So I want: having JSON string like this
{"Id":0, "Name":"Test User", "Roles": [{"Id":1, "Name": "Role 1"}, {"Id":2, "Name": "Role 2"}, {"Id":3, "Name": "Role 3"}]}
get entity with correctly deserialized Roles collection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,似乎没有人对这个问题感兴趣,无论如何,这是解决方案。此类序列化和反序列化自跟踪 POCO 实体,包括所有嵌套的 TrackableCollections 和对象。
请注意 SupportedTypes 方法。我添加了 IEntity 接口(内部空白),并在这一行修改了我的 T4 模板:
您无法对 IEntity 执行任何操作。只需根据需要编写 SupportedTypes 方法即可。
我还在 ChangeTracker 属性上方的模板中评论了 [DataMember] 属性:
无论如何,这并不重要。使用 EntityConverter 并享受吧。
Ok, seems like nobody was interested this question, anyway here is the solution. This class serializes and deserializes self-tracking POCO entities including all nested TrackableCollections and objects.
Please notice about SupportedTypes method. I added the IEntity interface (blank inside) and modified my T4 template on this line:
You can do nothing about IEntity. Just write the SupportedTypes method as you need.
I also commented the [DataMember] attribute in the template above the ChangeTracker property:
Anyway this is not important. Take the EntityConverter and enjoy.