0
我目前的實現遍歷特定項目中的所有類,使用SerializableAttribute
標記爲可序列化的所有類,並根據其他一些規則創建不同的XML模式文件,並反序列化手動創建XML文件。一切正常和順利,直到我添加的第一個委託到我的項目,像這樣:如何將代理標記爲不可序列化或忽略它?
public delegate void CharacterStoreChanged();
奇怪的是後盾類的編譯器的代表被自動標記爲可序列化,我在下面的代碼已經生成評論:
private static Type[] GetDatafileTypes()
{
Assembly assembly = Assembly.GetAssembly(typeof (Importer));
return
assembly.GetTypes()
.Where(t => t.IsSerializable && t.IsPublic)
.ToArray(); //contains the delegate's backing class since it's public and automatically serializable
}
private static T Deserialize<T>(string filePath)
{
Type[] dataFileTypes = GetDatafileTypes();
XmlSerializer x = new XmlSerializer(typeof (T), dataFileTypes); //Fails because we provide a class in dataFileTypes that doesn't have an empty constructor :-(
return (T)x.Deserialize(new XmlTextReader(filePath));
}
我想忽略代表(以及未來可能會發生的一切),但我不知道如何。我不能用[NonSerializable]
或[XmlIgnore]
來標記它,因爲這些不能應用於「聲明類型」,並且TypeInfo
上似乎沒有IsDelegate
方法可用於對它們進行過濾。
任何幫助,將不勝感激。
我再次想到的方式太複雜了。 – Lichtblitz