add custom type in settings.settings

Mr.Alexz picture Mr.Alexz · Sep 23, 2013 · Viewed 20k times · Source

I would like to use configuration file .settings to save this struct:

struct sR22Protocole
{
    Int32 inputkey;
    Int32 outputkey;
    Int32 voltage;
    Int32 Ohm;
    Int32 Correction;
};

In the settings designer, I can add different type but it doesn't show my struct in the browse section. Is there any way that the designer has access to my struct? If no, Is there any way to add it programmaticaly?

Answer

florien picture florien · Sep 14, 2016

Your type must have a System.Configuration.SettingsSerializeAsAttribute attribute. An enum parameter of type System.Configuration.SettingsSerializeAs specifies how the value will be serialized, the possible values are:

  • String
  • Xml
  • Binary
  • ProviderSpecific

Since this attribute can only be applied to class types, your own type has to be a class.

Secondly, the type must have a parameterless constructor. This is because a default instance of the type of the setting must be able to be assinged.

If you have just declared your class, the designer won't accept the type unless you have built your solution.