Storing generic List<CustomObject> using ApplicationSettingsBase
Asked Answered
Q

3

6

I am trying to save a List<Foo> using ApplicationSettingsBase, however it only outputs the following even though the list is populated:

<setting name="Foobar" serializeAs="Xml">
    <value />
</setting>

Foo is defined as follows:

[Serializable()]
public class Foo
{
    public String Name;
    public Keys Key1;
    public Keys Key2;

    public String MashupString
    {
        get
        {
            return Key1 + " " + Key2;
        }
    }

    public override string ToString()
    {
        return Name;
    }
}

How can I enable ApplicationSettingsBase to store List<Foo>?

Quadrillion answered 15/5, 2009 at 11:45 Comment(0)
A
11

Agreed with Thomas Levesque:

The following class was correctly saved/read back:

public class Foo
{
    public string Name { get; set; }

    public string MashupString { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

Note: I didn't need the SerializableAttribute.

Edit: here is the xml output:

<WindowsFormsApplication1.MySettings>
    <setting name="Foos" serializeAs="Xml">
        <value>
            <ArrayOfFoo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <Foo>
                    <Name>Hello</Name>
                    <MashupString>World</MashupString>
                </Foo>
                <Foo>
                    <Name>Bonjour</Name>
                    <MashupString>Monde</MashupString>
                </Foo>
            </ArrayOfFoo>
        </value>
    </setting>
</WindowsFormsApplication1.MySettings>

And the settings class I used:

sealed class MySettings : ApplicationSettingsBase
{
    [UserScopedSetting]
    public List<Foo> Foos
    {
        get { return (List<Foo>)this["Foos"]; }
        set { this["Foos"] = value; }
    }
}

And at last the items I inserted:

private MySettings fooSettings = new MySettings();

var list = new List<Foo>()
{
    new Foo() { Name = "Hello", MashupString = "World" },
    new Foo() { Name = "Bonjour", MashupString = "Monde" }
};

fooSettings.Foos = list;
fooSettings.Save();
fooSettings.Reload();
Active answered 15/5, 2009 at 12:48 Comment(1)
Great answer, in the final code example you don't need to specify private for fooSettings as this would run inside a function.Oracular
L
3

In XML serialization :

  • Fields are not serialized, only properties
  • Only public properties are serialized
  • Read-only properties are not serialized either

So there is nothing to serialize in your class...

Lipchitz answered 15/5, 2009 at 11:55 Comment(1)
Note that the List<Foo> returns the XMl fragment above. I know there are 9 Foo instances in the List being serialized, so why aren't there any indication that there are objects in the List?Quadrillion
D
1

Same here as Craig described. My Wrapper-Class did have a no-arg constructor but, it also has a generic List<T> where T didn't got a no-arg constructor so the save() failed either.

Like this:

public class foo {
    public List<bar> barList;

    public foo() {
        this.barList = new List<bar>();
    }
}

public class bar {
    public string baz;

    public bar(string baz) {
        this.baz = baz;
    }
}

Add a no-arg constructor to every custom-class wich you will use in the settings:

public bar() {
    this.baz = "";
}
Definitely answered 25/1, 2012 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.