这是我想要序列化并保存到存储的类。
public class Module : ObservableObject
{
private int id;
private MapLocation location;
[JsonProperty("id")]
public int Id
{
get { return id; }
set { Set(() => Id, ref id, value); }
}
public MapLocation Location
{
get { return location; }
set
{
Set(() => Location, ref location, value);
}
}
...
}
我的序列化和反序列化方法:
private void Save<T>(T data, string name)
{
var serializer = new DataContractSerializer(typeof(T));
string targetFileName = String.Format("{0}/{1}.dat", FolderName, name);
if (!storage.DirectoryExists(FolderName))
storage.CreateDirectory(FolderName);
try
{
using (var targetFile = storage.CreateFile(targetFileName))
{
serializer.WriteObject(targetFile, data);
}
}
catch (Exception ex)
{
storage.DeleteFile(targetFileName);
}
}
private void Delete(string name)
{
string targetFileName = String.Format("{0}/{1}.dat", FolderName, name);
if (storage.FileExists(targetFileName))
storage.DeleteFile(targetFileName);
}
private T Load<T>(string name)
{
T returnValue = default(T);
var serializer = new DataContractSerializer(typeof(T));
string targetFileName = String.Format("{0}/{1}.dat", FolderName, name);
if (storage.FileExists(targetFileName))
using (var sourceStream = storage.OpenFile(targetFileName, FileMode.Open))
{
returnValue = (T)serializer.ReadObject(sourceStream);
}
return returnValue;
}
正如您所看到的,我没有使用任何注释来进行xml序列化(由DataContractSerialier使用,JsonProperty用于从Server API进行序列化)。它适用于标准类型,如int,string,double和list以及collection,但它不会保存MapLocation类或我尝试过的任何其他类。我怎样才能解决这个问题?感谢