所以,我想保存物件,然后加載它并從中獲取資料。我創建了一個名為 SaveData 的類,在那里我有欄位 isVibrationOn。
作業代碼如下:
public class SaveData
{
public bool isVibratonOn;
}
下面是序列化的代碼:
public void SaveGame()
{
SaveData saveData = new SaveData();
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Path.Combine(Application.persistentDataPath, FILE_NAME));
SaveData(saveData);
bf.Serialize(file, saveData);
file.Close();
}
private void SaveData(SaveData saveData)
{
saveData.isVibrationOn = VibrationController.controller.isVibrationOn;
}
這是加載資料的代碼:
public void LoadGame()
{
if (File.Exists(Path.Combine(Application.persistentDataPath, FILE_NAME)))
{
FileStream file = File.Open(Path.Combine(Application.persistentDataPath, FILE_NAME), FileMode.Open);
if (file.Length > 0)
{
SaveData saveData = new SaveData();
BinaryFormatter bf = new BinaryFormatter();
saveData = (SaveData)bf.Deserialize(file);
LoadData(saveData);
file.Close();
}
}
}
public LoadData(SaveData saveData)
{
VibrationController.controller.isVibrationOn = saveData.isVibrationOn;
}
我的問題是,當我決定向 SaveData 物件添加或洗掉某些欄位(串列等)時,我的 LoadData 看起來會有所不同,但保存在設備上的物件也具有不同的欄位。簡單的例子
public LoadData(SaveData saveData)
{
VibrationController.controller.isVibrationOn = saveData.isVibrationOn;
//old save data doesn't have isMusicOn field
//LoadData method is different because I added new field on SaveData object after I saved file.
//This is simple example, but also it could be any changes like list of objects with an object that has different fields added/changed.
MusicController.controller.isMusicOn = saveData.isMusicOn;
}
我將如何檢查舊實體是否具有該欄位?
uj5u.com熱心網友回復:
一般建議不要使用 binaryFormatter。它速度慢、效率低、不安全且向后兼容性差。
因此,如果您更改該類,我不希望它可以對舊資料進行反序列化,更不用說告訴您缺少哪些欄位了。切換 .net 版本也可能是 binaryformatter 的問題。
那里有更好的序列化庫。Json.net 是基于文本的序列化的標準,我使用 protobuf.net 進行二進制序列化。但是還有許多其他庫可以使用。
要處理缺失或可選欄位,您通常會有一些默認值,例如 null,您可以檢查這些值。如果需要,還應該可以將欄位初始化為其他一些默認值。
我建議將序列化物件與域物件分開,因為序列化框架可能需要無引數建構式或公共設定器。單獨的序列化物件提供了管理版本之間物件結構差異的機會。
uj5u.com熱心網友回復:
如果您的目標是確保您的代碼不會因為“缺少”欄位而中斷:
如果您已經推出了“舊”版本,那就很難了 - 您將不得不實作某種從舊資料到新資料的“遷移”,可能是通過保持舊類原樣,在新類中實作您的更改(可能源自舊的,以將代碼重復保持在最低限度),然后檢查您正在反序列化的資料是舊的(我將其稱為“ MyClass_Old”)還是新的(“ MyClass_New”)。如果您能夠從某些元資料、檔案屬性等中確定這一點,那就太好了。如果沒有,你可以MyClass_New故意反序列化它并用 try-catch 包裝。如果您捕獲到 SerializationException,那么它可能是MyClass_Old,然后您將其反序列化為MyClass_Old,然后使用它來構造一個新MyClass_New實體。
但是,如果您尚未推出這些更改,則可以使用版本容忍序列化。您可以使用[OptionalFieldAttribute]等屬性來標記在不同版本中可能缺失的欄位、[OnDeserializingAttribute](在反序列化之前將呼叫的方法 - 可能在這些缺失的欄位中設定一些值)和[OnDeserializedAttribute]到“修復”或驗證您的反序列化物件。
因此,對于您的示例,知道您添加了該isMusicOn欄位,您會將其標記為可選(因為它可能在反序列化資料中丟失):
[OptionalField(VersionAdded = 2)]
bool isMusicOn;
然后將其設定為某種默認值(如果缺少)。假設您希望它默認開啟:
[OnDeserializing]
internal void OnDeserializingMethod(StreamingContext context)
{
isMusicOn = true;
}
請注意,這里您使用了“反序列化”屬性,因為如果該值沒有丟失,您確實希望保留該值。由于 OnDeserializingMethod在反序列化之前被呼叫,如果它存在,它將被反序列化的值覆寫。此處還介紹了這種特定情況(您有一個可能缺失的欄位,并且您希望它具有某些特定值)。
如果您的目標是檢查反序列化物件中是否缺少此特定欄位:
通過一些準備,同樣的原則(版本容忍序列化)也可用于專門確定該欄位是否存在于反序列化物件中。您可以在反序列化(使用[OnDeserializing])之前將其設定為某個預定值(序列化時不允許的值)。然后在反序列化后檢查該值是否仍然存在或已被合理的值替換。
因此,在您的情況下,假設 isMusicOn 的允許值是 true 和 false,那么您可以使 isMusicOn 成為可為空的布林值:
[OptionalField(VersionAdded = 2)]
bool? isMusicOn;
然后,您將通過使用[OnSerializing]確保序列化物件不能將 isMusicOn 設定為 null :
[OnSerializing()]
internal void OnSerializingMethod(StreamingContext context)
{
if (isMusicOn == null)
isMusicOn = false;
}
然后在標記為的方法中分配 null [OnDeserializing]:
[OnDeserializing]
internal void OnDeserializingMethod(StreamingContext context)
{
isMusicOn = null;
}
and then in [OnDeserialized] check if it is still null (if it was deserialized, it will have changed to proper value like true or false, if not it'll stay null):
[OnDeserialized]
internal void OnDeserializedMethod(StreamingContext context)
{
Console.WriteLine($"isMusicOn {isMusicOn == null ? "wasn't" : "was" } present in deserialized object!");
if (isMusicOn == null)
isMusicOn = false;//set it to some "proper" default value;
}
In this example, actually, nullable value wouldn't even require the setup (it'll be null by default even without the OnDeserializing part), but i'll leave it in as an example.
All this last part is probably more trouble then it's worth. Besides all the hassle with attributes, you have to change your field type to allow for that "super special value", and that might require you to change a lot of other code that depends on it. I suspect the real question is not "how to check if the field was there", but rather "how to make sure my app doesn't break because it wasn't there", and for that you don't need to know if your field was deserialized - just to make sure that it has some reasonable value in it after deserialization.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/340629.html
