简体   繁体   English

在 settings.settings 中添加自定义类型

[英]add custom type in settings.settings

I would like to use configuration file.settings to save this struct:我想使用配置 file.settings 来保存这个结构:

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?如果没有,有没有办法以编程方式添加它?

Your type must have a System.Configuration.SettingsSerializeAsAttribute attribute. 您的类型必须具有System.Configuration.SettingsSerializeAsAttribute属性。 An enum parameter of type System.Configuration.SettingsSerializeAs specifies how the value will be serialized, the possible values are: System.Configuration.SettingsSerializeAs类型的枚举参数指定值的序列化方式,可能的值为:

  • String
  • Xml XML
  • Binary 二进制
  • ProviderSpecific 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. 如果您刚刚申报了您的课程,设计师将不会接受该类型,除非您已经构建了解决方案。

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 http://www.blackwasp.co.uk/CustomAppSettings.aspx

Just go into the browse section and type your struct, class or enum name while prefixing it by the namespace of your type. 只需进入浏览部分,输入您的struct,class或enum name,同时为您的类型命名空间添加前缀。 Then, it would also be added in the dropdown for your next uses. 然后,它也会在下拉列表中添加,供您下次使用。

In your example: YourTypeNamespace.sR22Protocole 在您的示例中:YourTypeNamespace.sR22Protocole

It is actually not needed to add attributes etc. If you make sure that the class is serializable like Parameterless constructor and public properties, it will still work.实际上不需要添加属性等。如果您确保 class 像无参数构造函数和公共属性一样可序列化,它仍然可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM