如何创建自定义结构(或 class)类型的应用程序设置参数?

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

有这个结构:

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;
  }
}

如何在应用程序设置中存储这种类型的值?

该类型在 select 其类型的新参数列表中不可用,浏览输入完全限定的类型名称不起作用,因为未找到。

大部分教程和重复内容都不清楚,不完整,只提到类,很少结构。

主要来自这篇文章:Using Custom Classes with Application Settings

和各种搜索,我们需要添加SerializableTypeConverter属性:

[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;
  }
}

这是类型转换器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);
  }
}

接下来我们需要编译工程。

现在我们可以添加参数并选择其类型:

或使用底部菜单项浏览(如果列表中不可用)输入完全限定的类型名称:

能够写作:

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

所需的命名空间:

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

结果:

<?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>