У меня есть этот маленький метод в библиотеке классов, который хранится во внешней dll, вне приложения "client".
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
attribute. "} System.Exception
{System.Runtime.Serialization.InvalidDataContractException}
Я бы хотел знать, что означает ошибка, а не просто добавлять атрибуты и молиться, чтобы это сработало. Спасибо!
РЕДАКТИРОВАТЬ: По запросу, класс полностью.
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();
}
}