[英]Serialize object with an array in it
I have this class : 我有这个课:
[Serializable]
class Data
{
int data1;
int data2;
Other_Data[] array;
}
class Other_Data
{
// I have some ints and bool here
}
The question is how do I serialize/deserialize a object of type Data with a single file 问题是如何使用单个文件序列化/反序列化数据类型的对象
If the type Other_Data
is serializable too, then this should cause you no problems. 如果类型
Other_Data
也是可序列化的,那么这应该不会造成任何问题。 Just know that serializers require the types and properties or fields to be public: 只需知道序列化程序需要将类型和属性或字段公开即可:
[Serializable]
public class Data
{
public int data1;
public int data2;
public Other_Data[] array;
}
[Serializable]
public class Other_Data
{
public int someInt;
public bool someBool;
}
And then for example using the XmlSerializer : 然后例如使用XmlSerializer :
var obj = new Data() {
data1 = 5,
data2 = 7,
array = new Other_Data[] {
new Other_Data() { someInt = 1, someBool = true },
new Other_Data() { someInt = 2, someBool = true },
new Other_Data() { someInt = 3, someBool = false }
}
};
var xmlSerializer = new XmlSerializer(typeof(Data));
var stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, obj);
Console.WriteLine(stringWriter.ToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.