问题
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