简体   繁体   English

序列化的麻烦(参数对象的类型不是原始的)

[英]Trouble with serialization (The type of the argument object is not primitive)

I'm writing an evolutionary algorithm but I'm having trouble writing some items in an XML file. 我正在编写一个进化算法但是我在XML文件中编写一些项目时遇到了麻烦。 The error I'm recieving is 我收到的错误是

InvalidOperationException: The type of the argument object 'ExperimentSettings' is not primitive. InvalidOperationException:参数对象'ExperimentSettings'的类型不是原始的。

This is the code I'm working with (I left out some methods that work and are not related with the issue in any way): 这是我正在使用的代码(我遗漏了一些有效的方法,并且与这个问题无关):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class SerializerTest : MonoBehaviour {

    public GameObject spawner;

    // Use this for initialization
    void Start () {
        GetComponent<Button>().onClick.AddListener(delegate { Save(false, 0, 50, 5, 5, 50, 5, new Vector3(0, 20, 0), new System.IO.DirectoryInfo("/Experiments/Test #0"), spawner.GetComponent<CreatureGenerator>()); });
    }

    // Use this for initialization
    void Save(
        bool startCreatures,
        int experimentType,
        int totalCreaturesNo,
        int cycles,
        float experimentDuration,
        float survivorsPercentage,
        float probabilityOfMutation,
        Vector3 creaturesStartingPosition,
        DirectoryInfo saveFolder,
        CreatureGenerator generator
        )
    {
        //create file where to save the settings
        string settingsFile = Application.dataPath + saveFolder + "/settings_and_parameters.xml";

        if (File.Exists(settingsFile))
        {
            File.Delete(settingsFile);
        }

        XmlSerializer settingsSerializer = new XmlSerializer(typeof(Creature));
        FileStream settingsStream = new FileStream(settingsFile, FileMode.Create);

        //save the settings
        ExperimentSettings thisExperiment = new ExperimentSettings()
        {
            startCreatures = startCreatures,
            experimentType = experimentType,
            totalCreaturesNo = totalCreaturesNo,
            cycles = cycles,
            experimentDuration = experimentDuration,
            survivorsPercentage = survivorsPercentage,
            probabilityOfMutation = probabilityOfMutation,
            creaturesStartingPosition = creaturesStartingPosition,
            saveFolder = new DirectoryInfo(saveFolder.ToString()),
            experimentScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name
        };

        settingsSerializer.Serialize(settingsStream, thisExperiment);

        //close the file
        settingsStream.Close();
    }
}

In the Start function I just set up a button that when clicked saves a file with some arbitrary parameters. 在“开始”功能中,我只需设置一个按钮,单击该按钮可保存带有一些任意参数的文件。 I'm gonna leave that code there but it's not so pertinent with the error. 我要把那些代码留在那里,但它与错误不太相关。

This is the class that returns the error when serialized: 这是在序列化时返回错误的类:

[System.Serializable]
public class ExperimentSettings
{
    public bool startCreatures;
    public int experimentType;
    public int totalCreaturesNo;
    public int cycles;
    public float experimentDuration;
    public float survivorsPercentage;
    public float probabilityOfMutation;
    public Vector3 creaturesStartingPosition;
    public DirectoryInfo saveFolder;
    public string experimentScene;
}

The thing that baffles me is that I tried serializing another class in the same method in the same way it actually worked. 令我感到困惑的是,我尝试以同样的方式在同一方法中序列化另一个类,就像它实际工作一样。 The next code block is the class that's being serialized correctly: 下一个代码块是正确序列化的类:

[System.Serializable]
public class Creature
{
    public string creaturePrefab;
    public double result;
    public bool passed;
}

I tried removing all the variables from ExperimentSettings leaving an empty class but it still doesn't work. 我尝试从ExperimentSettings删除所有变量,留下一个空类,但它仍然不起作用。 I'm all out of ideas, any help is greatly appreciated! 我完全没有想法,非常感谢任何帮助!

Solved! 解决了! In the line 在线

 XmlSerializer settingsSerializer = new XmlSerializer(typeof(Creature)); 

I copy pasted from my own code but I forgot to change the class, so to solve this particular problem I'd have to edit this line this way 我从我自己的代码复制粘贴,但我忘了更改类,所以要解决这个特殊的问题,我必须这样编辑这一行

XmlSerializer settingsSerializer = new XmlSerializer(typeof(ExperimentSettings));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM