Maintain object references through Serialize/Deserialize

亡梦爱人 提交于 2019-12-31 02:18:24

问题


When serializing, I would like to serialize an object only once, then any references to that object should be serialized as a reference to the object. This is because, when I later deserialize the objects, I would like to maintain those references. To illustrate my goal, the code below should output "After Serialization: true".

class Program
{
    static void Main(string[] args)
    {
        MyObject obj = new MyObject() { Name = "obj" };
        MyObject[] myObjs = new MyObject[]
        {
            obj, obj
        };

        Console.WriteLine("Object Refs Equal?");
        Console.WriteLine(string.Format("Before Serialization: {0}",
            object.ReferenceEquals(myObjs[0], myObjs[1])));

        XmlSerializer toXml = new XmlSerializer(typeof(MyObject[]));
        using (FileStream toFile = File.Create(@"C:\foo.xml"))
        {
            toXml.Serialize(toFile, myObjs);
        }

        XmlSerializer fromXml = new XmlSerializer(typeof(MyObject[]));
        using (FileStream fromFile = File.OpenRead(@"C:\foo.xml"))
        {
            MyObject[] deserialized = (MyObject[])fromXml.Deserialize(fromFile);

            Console.WriteLine(string.Format("After Serialization: {0}",
            object.ReferenceEquals(deserialized[0], deserialized[1])));
        }

        Console.ReadLine();
    }
}

[Serializable]
public class MyObject
{
    public string Name { get; set; }
}

回答1:


If you're using .NET 3 or later, you should use the DataContractSerializer and set PreserveObjectReferences to true.




回答2:


You will have to do that by giving the objects identifiers other than their object reference.

An object's reference is essentially it's pointer. Since you cannot manipulate pointers directly in safe code must give the object some other unique name.




回答3:


Not sure if this is what you would like to hear... But lets just think about what you are trying to do.

You only have the need to serialize an object when: a) You want to store it somewhere an latter on retrieve the information. b) You want to send the object to another application.

A reference is nothing more than a pointer to where the object is. So in either case you simply cannot access the memory because in a) it is reserved for your application and in b) it is another machine and it doesn't even exists.

http://en.csharp-online.net/Glossary:Definition_-_Reference_type




回答4:


your code example won't work simply because the XmlSerializer does not save reference information, during the serialization process it will simply write the data for obj to the stream twice this means that when you deserialize it will read the data for obj as two different instances, it has no way of knowing that they referred to the same instance at serialization time. using the DataContractSerializer mentioned before should give you what you want but note that it uses non standard xml (see: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx)



来源:https://stackoverflow.com/questions/473182/maintain-object-references-through-serialize-deserialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!