简体   繁体   English

将自定义类序列化到文件

[英]Serializing a custom class to a file

I'm writing a save method for my Player class, I can serialize most fields using a binary writer however my custom class fields are not able to be serialized (i have a Race field for custom races) 我正在为Player类编写一个save方法,我可以使用二进制编写Player来序列化大多数字段,但是我的自定义类字段无法序列化(我有一个用于自定义种族的Race字段)

Since I am going to have a lot of different custom classes - like Race - I really need a good reliable way of saving this information. 由于我将拥有很多不同的自定义类(例如Race),因此我确实需要一种可靠的方法来保存此信息。

So, am I using the wrong kind of formatter? 那么,我使用的是错误的格式化程序吗? And if so, what is the one I should be using? 如果是这样,我应该使用什么? Alternatively, how do I write the method to serialize it? 或者,如何编写方法来序列化它? I have used the [Serializable()] attribute and implemented ISerializable interface where needed already. 我已经使用[Serializable()]属性并在需要的地方实现了ISerializable接口。

Here is the code: 这是代码:

public void Save()
{
    Stream stream = File.Open(SaveSlot, FileMode.Create);
    BinaryWriter writer = new BinaryWriter(stream);
    PlayerData saveData = new PlayerData();

    writer.Write(saveData.Name = PlayerData.player.Name);
    writer.Write(saveData.Race = PlayerData.player.Race);
    writer.Write(saveData.PlayClass = PlayerData.player.PlayClass);
    writer.Write(saveData.Level = PlayerData.player.Level);
    writer.Write(saveData.Experience = PlayerData.player.Experience);
    writer.Write(saveData.Strength = PlayerData.player.Strength);
    writer.Write(saveData.Dexterity = PlayerData.player.Dexterity);
    writer.Write(saveData.Constitution = PlayerData.player.Constitution);
    writer.Write(saveData.Intelligence = PlayerData.player.Intelligence);
    writer.Write(saveData.Wisdom = PlayerData.player.Wisdom);
    writer.Write(saveData.Charisma = PlayerData.player.Charisma);
    writer.Write(saveData.Wounds = PlayerData.player.Wounds);
    writer.Write(saveData.MaxWounds = PlayerData.player.MaxWounds);
    writer.Write(saveData.Attackbonus = PlayerData.player.Attackbonus);
    writer.Write(saveData.Defensebonus = PlayerData.player.Defensebonus);

    writer.Close();
    Console.WriteLine("Data Saved Successfully");
}

Like I said in my comment you can use the json format for this. 就像我在评论中所说,您可以为此使用json格式。

Sample class 样品课

public class MyObject
{
    public string MyProperty { get; set; }
}

Sample data 样本数据

List<MyObject> objects = new List<MyObject>
{
    new MyObject{MyProperty = "test 1"},
    new MyObject{MyProperty = "test 2"},
    new MyObject{MyProperty = "test 3"}
};

Sample code 样例代码

Then you can use this code for serialize your data (using Newtonsoft ). 然后,您可以使用此代码来序列化数据(使用Newtonsoft )。

string json = JsonConvert.SerializeObject(objects);

The content of json will be json的内容将是

[{"MyProperty":"test 1"},{"MyProperty":"test 2"},{"MyProperty":"test 3"}] [{“ MyProperty”:“ test 1”},{“ MyProperty”:“ test 2”},{“ MyProperty”:“ test 3”}]

If you want to deserialize the string use this. 如果要反序列化字符串,请使用此方法。

List<MyObject> deserialzed = JsonConvert.DeserializeObject<List<MyObject>>(json);

More info 更多信息

You can save the json string to a file and load and deserialize it when you need it. 您可以将json字符串保存到文件中,并在需要时进行加载和反序列化。 This works with every type of a property including list and custom objects. 这适用于每种类型的属性,包括列表和自定义对象。

Have a read at the JSON format for more information. 阅读JSON格式以了解更多信息。

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

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