.NET XmlSerializer 和通用 SortedSet 属性

发布于 2025-01-02 06:18:02 字数 429 浏览 2 评论 0原文

我有一个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花之痕靓丽 2025-01-09 06:18:02

尝试将 setter 更改为 public。

XmlSerializer 不处理私有或受保护的字段。

Try to change setter to public.

XmlSerializer does not handle private or protected fields.

﹉夏雨初晴づ 2025-01-09 06:18:02

试试这个:

//
//some example class
//
public class Music: IComparable<Music>
{
    public Music() { }
    public string Author { get; set; }
    public string Name { get; set; }

    public int CompareTo(Music other)
    {
        if (Author == other.Author)
        {
            return Name.ToUpper().CompareTo(other.Name.ToUpper());
        }
        else
        {
            return Author.ToUpper().CompareTo(other.Author.ToUpper());
        }
    }
}

//
// that is to deserialize
//
var musics = new SortedSet<Music>();
var mPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\j.txt";
if (File.Exists(mPath))
{
    File.Copy(mPath, mPath.Replace(".txt", DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".txt"));
    var ser = new XmlSerializer(typeof(List<Music>));
    using (var sr = new StreamReader(mPath))
    {
        musics = new SortedSet<Music>((List<Music>)ser.Deserialize(sr));
    }
}

//
// that is to serialize
//
var ser = new XmlSerializer(typeof(List<Music>));

using (var wri = new StreamWriter(mPath))
{
    ser.Serialize(wri, musics.ToList());
}

Try this:

//
//some example class
//
public class Music: IComparable<Music>
{
    public Music() { }
    public string Author { get; set; }
    public string Name { get; set; }

    public int CompareTo(Music other)
    {
        if (Author == other.Author)
        {
            return Name.ToUpper().CompareTo(other.Name.ToUpper());
        }
        else
        {
            return Author.ToUpper().CompareTo(other.Author.ToUpper());
        }
    }
}

//
// that is to deserialize
//
var musics = new SortedSet<Music>();
var mPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\j.txt";
if (File.Exists(mPath))
{
    File.Copy(mPath, mPath.Replace(".txt", DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".txt"));
    var ser = new XmlSerializer(typeof(List<Music>));
    using (var sr = new StreamReader(mPath))
    {
        musics = new SortedSet<Music>((List<Music>)ser.Deserialize(sr));
    }
}

//
// that is to serialize
//
var ser = new XmlSerializer(typeof(List<Music>));

using (var wri = new StreamWriter(mPath))
{
    ser.Serialize(wri, musics.ToList());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文