Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save/serialize a custom class to the settings file?

Tags:

I have a small class that holds two strings as follows:

    public class ReportType     {         private string displayName;         public string DisplayName         {             get { return displayName; }         }          private string reportName;         public string ReportName         {             get { return reportName; }         }          public ReportType(string displayName, string reportName)         {             this.displayName = displayName;             this.reportName = reportName;         }     } 

I want to save an instance of this class to my settings file so that I can do the following:

ReportType reportType = Settings.Default.SelectedReportType; 

Googling seems to suggest that it is possible but there doesn't appear to be a clear guide anywhere for me to follow. I understand that some serialization is required but don't really know where to begin. Also, when I go into the Settings screen in Visual Studio and click "browse" under the Type column there is no option to select the current namespace which contains the ReportType class.

like image 634
Calanus Avatar asked Aug 24 '09 09:08

Calanus


People also ask

What is custom serialization in C#?

Custom serialization is the process of controlling the serialization and deserialization of a type. By controlling serialization, it's possible to ensure serialization compatibility, which is the ability to serialize and deserialize between versions of a type without breaking the core functionality of the type.

Can we serialize static class?

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI). But, static variables belong to class therefore, you cannot serialize static variables in Java.


2 Answers

OK I think that I have eventually worked it out. The first thing to do is to add the following attributes to each property of the ReportType class that needs to be serialised and inherit the class from ApplicationSettingsBase:

    public class ReportType : ApplicationSettingsBase     {         private string displayName;         [UserScopedSetting()]         [SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]         public string DisplayName         {             get { return displayName; }         } 

..............

and then, once you have rebuilt your assembly (important!) you can go into the settings screen and click browse and then type your namespace and class name in the text box at the bottom (e.g. Label_Creator.ReportType). The namespace and class name do not appear in the tree and so this part is not exactly obvious what you need to do which is why it is a bit confusing....

like image 187
Calanus Avatar answered Sep 24 '22 11:09

Calanus


@Calanus solution did not work for me as-is (on Visual Studio 2015). The missing step is actually setting or getting from the actual settings. As for the original question, implementing a simple POCO can be achieved like this:

[Serializable] public class ReportType {     public string DisplayName { get; set; }     public string ReportName { get; set; }      public ReportType() { }      public ReportType(string displayName, string reportName)     {         DisplayName = displayName;         ReportName = reportName;     } }  // the class responsible for reading and writing the settings public sealed class ReportTypeSettings : ApplicationSettingsBase {     [UserScopedSetting]     [SettingsSerializeAs(SettingsSerializeAs.Xml)]     [DefaultSettingValue("")]     public ReportType ReportType     {         get { return (ReportType)this[nameof(ReportType)]; }         set { this[nameof(ReportType)] = value; }     } } 

I have used for actually serialization a list:

[Serializable] public class Schedule {     public Schedule() : this(string.Empty, DateTime.MaxValue)     {     }      public Schedule(string path, DateTime terminationTime)     {         path = driverPath;         TerminationTime = terminationTime;     }      public DateTime TerminationTime { get; set; }     public string Path { get; set; } }  public sealed class Schedules : ApplicationSettingsBase {     [UserScopedSetting]     [SettingsSerializeAs(SettingsSerializeAs.Xml)]     [DefaultSettingValue("")]     public List<Schedule> Entries     {         get { return (List<Schedule>)this[nameof(Entries)]; }         set { this[nameof(Entries)] = value; }     } } 

Instantiate a Schedules (ReportTypeSettings) object. It will automatically read the settings. You can use Reload method to refresh. For instance:

ReportTypeSettings rts = new ReportTypeSettings(); rts.Reload(); rts.ReportType = new ReportType("report!", "report1"); rts.Save(); 

IMPORTANT NOTES:

  1. Note that UserScoped is intentionally used. ApplicationScoped behaves differently, so make sure you know what you are doing.
  2. The members are public (including the setter), and there's a default constructor even though it was needed by the code. However, serialization using XML didn't work properly. Due to time constraints I didn't investigate.
  3. You can change serialization to binary format as well. It will use BASE64 to store the data.
  4. All the settings is stored in LOCAL APP DATA folder, in a text file.
like image 31
Guy Avatar answered Sep 25 '22 11:09

Guy