这是序列化错误还是数据契约错误

发布于 2024-10-10 06:47:01 字数 3921 浏览 0 评论 0原文

我在一个类库中有这个小方法,该方法保存在外部 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 属性,并标记其所有 您想要序列化的成员 数据成员属性 属性。”} 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();
    }
}

I have this little method in a class library that is held in an external dll, away from the "client" app.

    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();
    }

Essentially it stores itself to the phones isolated storage. The class that contains this is not marked with any attributes. The error I get is this:

ex {"Type 'CabbageWrapper.Account'
cannot be serialized. Consider marking
it with the DataContractAttribute
attribute, and marking all of its
members you want serialized with the
DataMemberAttribute
attribute."} System.Exception
{System.Runtime.Serialization.InvalidDataContractException}

I would like to know what the error means rather than just adding attributes and praying that this works. Thanks!

EDIT: As requested, class in full.

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佼人 2024-10-17 06:47:01

异常消息非常不言自明。通过用这些属性装饰成员,您可以告诉序列化器序列化哪些项目(大多数时候您只想序列化某些成员,而不是类中的所有成员)。您必须定义它们的原因很简单,您可以决定序列化什么和不序列化什么。还有性能、安全性等考虑,所以这就是默认情况下不这样做的原因。阅读 http://msdn.microsoft.com/en- us/library/system.runtime.serialization.datacontractserializer.aspx“将 DataContractAttribute 属性应用于类,将 DataMemberAttribute 属性应用于类成员,以指定序列化的属性和字段。”

The exception message is pretty self-explanatory. By decorating members with these attributes, you tell serializer what items are serialized (most of the time you want serialize only some members, not everything in your class). The reason why you must define them is simply that you can decide what to serialize and what not. There are also performance, security etc considerations, so this is reason why it's not done by default. Read http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx "Apply the DataContractAttribute attribute to classes, and the DataMemberAttribute attribute to class members to specify properties and fields that are serialized. "

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文