XMLSerialize 异常 [英] XMLSerialize Exception

查看:17
本文介绍了XMLSerialize 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在序列化一个类,但出现以下异常:

I am serializing a class and I get the following exception:

您必须在 System.Configuration.SettingsPropertyCollection 上实现默认访问器,因为它继承自 ICollection.

You must implement a default accessor on System.Configuration.SettingsPropertyCollection because it inherits from ICollection.

当以下行被执行时:

XmlSerializer xs = new XmlSerializer(typeof(CustomConfiguration));

有什么帮助吗?

public class CustomConfiguration : ConfigurationObjectBase 
{ 

         public CustomConfiguration () { //DO NOTHING. } 

         [User] 
         public uint Version 
         { get { return ((uint)(this["Version"])); } 
           set { this["Version"] = value; } 
}

} 

ConfigurationObjectBase 派生自 System.configuration.ApplicationSettingsBase.

推荐答案

"default accessor" 是根据它的索引.例如:

"default accessor" is the special property that returns an object of the collection based on its index. for example:

[Serializable()]
public class IntList : ICollection {

    // Default Accessor Implementation
    public int this[int index] {
        get {

            return 0;
        }
        set { /* Do Nothing */ }
    }
}

因此无法在现有的第三方类中实现.对 ApplicationSettings 使用 xml 序列化是非常糟糕的主意,请使用 Save()、Reload() 和 Reset() 方法,或者使用您自己的不是从 ApplicationSettingsBase CustomConfiguration 类派生的方法.

Therefore unavailable to implement that in existing third-party class. Using xml serializing for ApplicationSettings is very bad idea, use Save(), Reload() and Reset() methods, or use your own not derived from ApplicationSettingsBase CustomConfiguration classes.

这篇关于XMLSerialize 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆