.NET XmlSerializer 和通用 SortedSet 属性
我有一个 .net 配置类:
public class Config {
public SortedSet<string> SiteURLs { get; private set; }
public Config() {
SiteURLs = new SortedSet<string>();
}
}
我试图将其发送到 XmlSerializer,但失败并显示“存在反映类型的错误”。将 [XmlIgnore] 添加到 SiteURLs 属性允许对类进行序列化。
我真的必须在这里编写自定义序列化代码吗?文档表明,只要该属性实现 ICollection 并提供“Add”方法,它就应该可以工作。也许我缺少一些其他必要的配置。
如何使用 .NET 序列化/反序列化泛型的示例似乎到处都有自定义序列化。
I have a .net config class:
public class Config {
public SortedSet<string> SiteURLs { get; private set; }
public Config() {
SiteURLs = new SortedSet<string>();
}
}
I'm trying to send it out to an XmlSerializer, which fails with "There was an error reflecting type". Adding [XmlIgnore] to the SiteURLs property allows the class to be serialized.
Do I actually have to write custom serialization code here? The docs indicate that as long as the property implements ICollection and provides an 'Add' method, it should work. Perhaps I am missing some other necessary configuration.
Examples of how to serialize/deserialize generics with .NET all seem to have custom serialization all over the place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 setter 更改为 public。
XmlSerializer
不处理私有或受保护的字段。Try to change setter to public.
XmlSerializer
does not handle private or protected fields.试试这个:
Try this: