How to create an application settings parameter of type a custom struct (or class)?

梦想的初衷 提交于 2021-02-08 07:43:47

问题


Having this struct:

public struct MyStruct
{
  public int MyInt { get; set; }
  public bool MyBool { get; set; }
  public string MyString { get; set; }
  public MyStruct(int myint, bool mybool, string mystring)
  {
    MyInt = myint;
    MyBool = mybool;
    MyString = mystring;
  }
}

How to store a value of this type in the application settings?

The type is not available in the list of the new parameter to select its type and browing to enter the fully qualified type name does not work as it is not found.

Most of tutorials and duplicates are unclear, incomplete and only mention classes, rarely structures.


回答1:


Mainly from this article: Using Custom Classes with Application Settings

And various searches, we need to add Serializable and TypeConverter attributes:

[Serializable]
[TypeConverter(typeof(MyStructConverter))]
public struct MyStruct
{
  public int MyInt { get; set; }
  public bool MyBool { get; set; }
  public string MyString { get; set; }
  public MyStruct(int myint, bool mybool, string mystring)
  {
    MyInt = myint;
    MyBool = mybool;
    MyString = mystring;
  }
}

Here is the type converter class:

public class MyStructConverter : TypeConverter
{
  public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  {
    return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
  }
  public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  {
    return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
  }
  public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  {
    if ( value is string )
    {
      string[] parts = ( (string)value ).Split(new char[] { ';' });
      var instance = new MyStruct();
      instance.MyInt = parts.Length > 0 ? Convert.ToInt32(parts[0]) : 0;
      instance.MyBool = parts.Length > 1 ? Convert.ToBoolean(parts[1]) : false;
      instance.MyString = parts.Length > 2 ? Convert.ToString(parts[2]) : "";
      return instance;
    }
    return base.ConvertFrom(context, culture, value);
  }
  public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  {
    if ( destinationType == typeof(string) )
    {
      var instance = (MyStruct)value;
      return string.Format("{0};{1};{2}", instance.MyInt, instance.MyBool, instance.MyString);
    }
    return base.ConvertTo(context, culture, value, destinationType);
  }
}

Next we need to compile the project.

Now we can add the parameter and choose its type:

Or use the bottom menu item Browse if not available in the list to enter the fully qualified type name:

To be able to write:

var myParam = Properties.Settings.Default.MyParam;
myParam.MyInt = 10;
myParam.MyBool = true;
myParam.MyString = "Test";
Properties.Settings.Default.MyParam = myParam;
Properties.Settings.Default.Save();

Required namespaces:

using System;
using System.ComponentModel;
using System.Globalization;

Result:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <WindowsFormsAppTest.Properties.Settings>
            <setting name="MyParam" serializeAs="String">
                <value>10;True;Test</value>
            </setting>
        </WindowsFormsAppTest.Properties.Settings>
    </userSettings>
</configuration>


来源:https://stackoverflow.com/questions/65850497/how-to-create-an-application-settings-parameter-of-type-a-custom-struct-or-clas

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