为什么在序列化ObservableCollection< T>?时无法添加属性? [英] Why can't I add attributes when serializing ObservableCollection<T>?

查看:145
本文介绍了为什么在序列化ObservableCollection< T>?时无法添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一些自定义属性扩展ObservableCollection并让它序列化。但是,我似乎无法让它序列化这些属性。我正在使用.NET 4.0,他们修复了ObservableCollection的序列化问题,但仍然遇到问题。我的预感是在基类上调用GetObjectData而不是我的。有什么想法吗?

I'm trying to extend an ObservableCollection with a few custom properties and have it serialize. However, I can't seem to get it to serialize these properties. I'm using .NET 4.0 where they fixed the serialization issues of ObservableCollection, but am still having problems. My hunch is that GetObjectData is being called on the base class and not mine. Any ideas?

[Serializable]
[XmlRoot(ElementName = "MyCollection")]
public class MyCollection : ObservableCollection<MyItem>, ISerializable
{
    private string name;

    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Name", Name);
    }

    private MyCollection()
    {
        Name = string.Empty;
    }

    public MyCollection(string name)
    {
        Name = name;
    }

    public MyCollection(SerializationInfo info, StreamingContext context)
    {
        Name = (string)info.GetValue("Name", typeof(string));
    }

    [XmlAttribute]
    public string Name
    {
        get { return name; }
        protected set
        {
            string originalName = name;
            name = value;
            if (originalName != name)
                OnPropertyChanged(new PropertyChangedEventArgs("Name"));
        }
    }

    public void SaveToFile(string path)
    {
        string directory = Path.GetDirectoryName(path);
        if (!Directory.Exists(directory))
            Directory.CreateDirectory(directory);

        XmlSerializer serializer = new XmlSerializer(typeof(MyCollection));
        using (TextWriter textWriter = new StreamWriter(path))
        {
            serializer.Serialize(textWriter, this);
            textWriter.Close();
        }
    }

    public static MyCollection LoadFromFile(string path)
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(MyCollection));
        using (TextReader textReader = new StreamReader(path))
        {
            MyCollection myCollection = (MyCollection)deserializer.Deserialize(textReader);
            textReader.Close();
            return myCollection;
        }
    }
}


推荐答案

XML序列化不支持此方案。您根本无法向实现 ICollection 的类添加任何内容。

XML Serialization does not support this scenario. You simply cannot add anything to a class implementing ICollection.

如果你需要这个,那么你必须实现 IXmlSerializable 并自己完成工作。

If you require this, then you will have to implement IXmlSerializable and do the work yourself.

请注意,您可能会将XML序列化与运行时序列化混淆。 XML Serialization不关心 [Serializable] 属性或 GetObjectData 等。

Note that you may be confusing XML Serialization with runtime serialization. XML Serialization doesn't care about the [Serializable] attribute or GetObjectData, etc.

这篇关于为什么在序列化ObservableCollection&lt; T&gt;?时无法添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆