我正在编写进化算法,但在 XML 文件中写入某些项目时遇到问题。我收到的错误是
InvalidOperationException: The type of the argument object 'ExperimentSettings' is not primitive.
这是我正在使用的代码(我遗漏了一些有效但与问题无关的方法):
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();
}
}
在“开始”功能中,我只是设置了一个按钮,单击该按钮可保存带有一些任意参数的文件。我会将该代码留在那里,但它与错误无关。
这是序列化时返回错误的类:
[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;
}
让我感到困惑的是,我尝试以与实际工作相同的方式在同一方法中序列化另一个类。下一个代码块是正确序列化的类:
[System.Serializable]
public class Creature
{
public string creaturePrefab;
public double result;
public bool passed;
}
我尝试从 ExperimentSettings 中删除所有变量,留下一个空类,但它仍然不起作用。我完全没有想法,非常感谢任何帮助!
最佳答案
解决了! 在行中
XmlSerializer settingsSerializer = new XmlSerializer(typeof(Creature));
我从我自己的代码中复制粘贴但我忘记更改类,所以要解决这个特定问题我必须以这种方式编辑这一行
XmlSerializer settingsSerializer = new XmlSerializer(typeof(ExperimentSettings));
关于c# - 序列化问题(参数对象的类型不是原始类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48242046/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search