"クライアント"アプリケーションから離れた外部DLLに保持されているクラスライブラリにこの小さなメソッドがあります。これはシリアライゼーションエラーかデータコントラクトエラーですか?
public void SaveToDisk()
{
// Create a storage container and save
// this instance to it. Use "this" storage name.
var settingsToStore = this;
var settings = IsolatedStorageSettings.ApplicationSettings;
settings[StorageName] = settingsToStore;
settings.Save();
}
本質的に、電話機隔離されたストレージに自分自身を格納します。これを含むクラスには、属性が設定されていません。私が手にエラーがこれです:
EX { "タイプ『CabbageWrapper.Account』 シリアライズすることはできませんあなたが と、シリアライズしたいDataContractAttribute 属性でそれを をマークし、その メンバーのすべてをマーキング考えてみましょう。私はエラーではなく、単に属性を追加し、これがめざしていることを祈っよりも、何を意味するのか知っていただきたいと思いDataMemberAttribute 属性。 "}のSystem.Exception {} System.Runtime.Serialization.InvalidDataContractException
rks。ありがとう!
EDIT:要求されたとおりに、完全にクラスします。
using System.IO.IsolatedStorage;
public class Account
{
public string Provider { get; private set; }
public string ServerSymbol { get; private set; }
public int MessageCharAllowance { get; private set; }
public int RemainingWebtextAllowance { get; set; }
public int WebtextAllowance { get; private set; }
public string Username { get; private set; }
public string Password { get; private set; }
public string StorageName { get; private set; }
public Account(string provider, string storageName, string username, string password)
{
// Assign the values to "this" instance.
Provider = provider;
Username = username;
Password = password;
StorageName = storageName;
// Load the ServerSymbol and WebtextAllowance from
// the libraries resources. These are known values.
PopulateKnownData();
// Save to disk
SaveToDisk();
}
public Account(string storageName)
{
// We need to know the name of the storage
// container to perform the load. Get it
// from the caller and save it to "this" instance.
StorageName = storageName;
// Perform the load.
LoadFromDisk();
}
private void PopulateKnownData()
{
switch (Provider)
{
case "Vodafone":
ServerSymbol = "v";
WebtextAllowance = 600;
RemainingWebtextAllowance = -1;
break;
case "O2":
ServerSymbol = "o";
WebtextAllowance = 250;
RemainingWebtextAllowance = -1;
break;
case "Meteor":
ServerSymbol = "m";
WebtextAllowance = 250;
RemainingWebtextAllowance = -1;
break;
case "Three":
ServerSymbol = "t";
WebtextAllowance = 333;
RemainingWebtextAllowance = -1;
break;
default:
break;
}
}
public void LoadFromDisk()
{
// Create a dummy account for rehydration and
// use it to grab the stored account from memory.
Account storedAccount;
IsolatedStorageSettings.ApplicationSettings.TryGetValue(StorageName, out storedAccount);
// Use the stored details to hydrate this instance.
Provider = storedAccount.Provider;
ServerSymbol = storedAccount.ServerSymbol;
RemainingWebtextAllowance = storedAccount.RemainingWebtextAllowance;
WebtextAllowance = storedAccount.WebtextAllowance;
MessageCharAllowance = storedAccount.MessageCharAllowance;
Username = storedAccount.Username;
Password = storedAccount.Password;
}
public void SaveToDisk()
{
// Create a storage container and save
// this instance to it. Use "this" storage name.
var settingsToStore = this;
var settings = IsolatedStorageSettings.ApplicationSettings;
settings[StorageName] = settingsToStore;
settings.Save();
}
}
なぜシリアル化できないのかを知るには、CabbageWrapper.Accountクラスを参照する必要があります。 –
私は編集します。 – deanvmc