我使用MonoTouch的工作,一個iPhone的項目,我需要序列化和保存一個簡單的對象屬於C#類與CLLocation類型的數據成員:MonoTouch:如何序列化未標記爲可序列化的類型(如CLLocation)?
[Serializable]
public class MyClass
{
public MyClass (CLLocation gps_location, string location_name)
{
this.gps_location = gps_location;
this.location_name = location_name;
}
public string location_name;
public CLLocation gps_location;
}
這是我的二進制序列化方法:
static void SaveAsBinaryFormat (object objGraph, string fileName)
{
BinaryFormatter binFormat = new BinaryFormatter();
using (Stream fStream = new FileStream (fileName, FileMode.Create, FileAccess.Write, FileShare.None)) {
binFormat.Serialize (fStream, objGraph);
fStream.Close();
}
}
但是,當我執行此代碼(myObject的是上面的類的實例):
try {
SaveAsBinaryFormat (myObject, filePath);
Console.WriteLine ("object Saved");
} catch (Exception ex) {
Console.WriteLine ("ERROR: " + ex.Message);
}
我得到這個例外? n:
ERROR: Type MonoTouch.CoreLocation.CLLocation is not marked as Serializable.
有沒有辦法用CLLocation序列化一個類?
謝謝,工作就像一個魅力! –