add custom type in settings.settings

感情迁移 提交于 2020-01-03 07:24:20

问题


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?


回答1:


I believe the class (or struct?) must be able to be serialized to use in the settings file. I followed this blog post when I did this for a default object in an application:

http://www.blackwasp.co.uk/CustomAppSettings.aspx




回答2:


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.




回答3:


Just go into the browse section and type your struct, class or enum name while prefixing it by the namespace of your type. Then, it would also be added in the dropdown for your next uses.

In your example: YourTypeNamespace.sR22Protocole



来源:https://stackoverflow.com/questions/18967575/add-custom-type-in-settings-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!