Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
737 views
in Technique[技术] by (71.8m points)

c# - XMLSerialize Exception

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...