开发者

XMLSerialize Exception

开发者 https://www.devze.com 2022-12-21 09:03 出处:网络
I am serializing a class and I get the following exception: 开发者_运维技巧 You must implement a default accessor on System.Configuration.SettingsPropertyCollection because it inherits from ICollec

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

开发者_运维技巧

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

when the following line is executed:

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

Any help?

public class CustomConfiguration : ConfigurationObjectBase 
{ 

         public CustomConfiguration () { //DO NOTHING. } 

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

} 

ConfigurationObjectBase is derived from System.configuration.ApplicationSettingsBase.


"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 */ }
    }
}

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.


you have to have a default constructor, that is, one that does not take any parameters, like so....

class foo
{
    public foo() {}
}

if the class you are trying to serialize does not have one, XMLSerializer throws that exception. If you are trying to serialize a built-in class, you are going to have to derive your own or create a wrapper class.

0

精彩评论

暂无评论...
验证码 换一张
取 消