Kyle Williamson Kyle Williamson - 3 years ago 118
C# Question

Why is this generic list not being saved in application settings?

I am saving and loading various variables in my application settings file (Settings.settings). Saving/Loading variables such as

strings
,
Uris
and
DataTables
is working correctly.

When attempting to Save/Load a custom object
List<IterationFilter>
, the List is lost when the application is closed and reopened.
List<IterationFilters>
becomes
null
when the application is reloaded... even though an
IterationFilter
was added to the List and saved.

Saving a String (working correctly):

Properties.Settings.Default.ConnectionString = connectionString;
Properties.Settings.Default.Save();


Saving a Generic List:

Properties.Settings.Default.FilterList.Add(newFilter);
Properties.Settings.Default.Save();


I followed this answer, to create my List setting. My .settings file look like this:

enter image description here

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public System.Collections.Generic.List<TFS_Extraction.IterationFilter> FilterList {
get {
return ((System.Collections.Generic.List<TFS_Extraction.IterationFilter>)(this["FilterList"]));
}
set {
this["FilterList"] = value;
}
}


My IterationFilter class:

namespace TFS_Extraction
{
[Serializable]
public class IterationFilter
{
public string Operator { get; set; }
public string Value { get; set; }


public IterationFilter(string _operator, string _value)
{
Operator = _operator;
Value = _value;
}
}

Answer Source

TFS_Extraction.IterationFilter has to be serializable. The class is required to have a public default constructor.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download