如何序列化包含其他类的对象类(递归序列化?)递归、序列化、对象

2023-09-03 16:04:51 作者:兽性打败理性

我怎样才能做到这一点?或将串行自动递归去,和所有子对象序列化为XML?

How can I do this? Or will the serializer automatically go with recursion, and serialize all those child objects into XML?

给我你将如何序列化包含在自身其他类的对象类的例子!这是这个问题的核心!

我想这一点,才没有输出的什么(XML头部除外)到目标XML文件中。

I've tried this, and it didn't output anything (except the XML header) to the targeted XML file.

我的问题是,我需要序列化一个简单的类,只是持有List对象。但是,这些实体也HOD List对象。 (另一个好处是,如果我能避免一些部件的序列化,因为某些衍生并有字典在其中)。

My problem is that I need to serialize a simple class, that just holds a List object. But, those Entities also hod List objects. (Another plus would be if I could avoid the serialization of some components, because some are derived and have dictionaries in them).

public void SaveCurrent(string MapFileName)
{
    string MapPath = world_.game_.Content.RootDirectory + "/Maps/" + MapFileName + ".xml";
    StreamWriter MapWriter = new StreamWriter(MapPath);

    Map SavedMap = new Map();
    SavedMap.Entities = world_.Entities;
    XmlSerializer xSerializer = new XmlSerializer(SavedMap.GetType());

    xSerializer.Serialize(MapWriter, SavedMap);
    MapWriter.Close();
}

这就是那块code,做序列化。

That's the piece of code that does the serialization.

public class Map
{
    internal string MapName;
    internal string MapDescription;
    internal string MapAuthor;
    public List<Entity> Entities = new List<Entity>();
}

这是多数民众赞成序列化的类。 难道内部被算作公众,如果序列是从同一个组件叫什么名字?的code。在 SavedMap.GetType()功能,我尝试了 typeof运算(图)过,但没有成功。我想这是因为我需要一些其他的方法来处理每一个新的类(深序列化)我该怎么做呢?

And this is the class that's serialized. Could internals be counted as publics, if the serialization is called from the same assembly? The code throws exception at the SavedMap.GetType() function, and I've tried typeof(Map) too, but without success. I guess it's because I need some other way to handle each new class (deep serialization) how do I do that?

另外,我发现了一些例子,不存在接口继承或属性,所以我没加的两种,但我打算使用IXmlSerializable的,虽然我不知道该怎么称呼另一序列化实施中WriteXML里面。

Also, I've found on some examples, that there are no interface inheritance or attributes, therefore I didn't add those either, but I'm planning to use IXmlSerializable, though I don't know how to call another serialization inside WriteXML implementation.

推荐答案

关于类型的问题,约什 - 爱因斯坦mentionned,你不必与XmlInclude属性的工作:你也可以在类型列表传递给串行(签字是的XmlSerializer(类型基本类型,类型[] extraTypes))。这应该做的,特别是如果有一个机会,额外的类型列表随时间增长。

About the type problem that Josh Einstein mentionned, you do not have to work with the XmlInclude attribute : you can also pass the list of types to the serializer (signature being XmlSerializer(Type baseType, Type[] extraTypes)). This should be done especially if there's a chance that the extra types list grow over time.

查找额外的类型,可通过反射在启动或者完成了要序列化的对象或反射的加载组件来获取所需的任何类型的。

Finding the extra-types can be either done through reflection over the object to be serialized or reflection at startup on the loaded assemblies to fetch any needed types.

编辑:原材料例如:

public abstract class Animal
{
}

public class Dog : Animal
{
}

public class Cat : Animal
{
}

public static class AnimalSerializer
{
    public static void Serialize(List<Animal> animals, Stream stream)
    {
        List<Type> animalTypes = new List<Type>();
        foreach (Animal animal in animals)
        {
            Type type = animal.GetType();
            if (!animalTypes.Contains(type))
            {
                animalTypes.Add(type);
            }
        }
        XmlSerializer serializer = new XmlSerializer(typeof(List<Animal>), animalTypes.ToArray());
        serializer.Serialize(stream, animals);
    }
}