【问题标题】:How to create an application settings parameter of type a custom struct (or class)?如何创建自定义结构(或类)类型的应用程序设置参数?
【发布时间】:2021-01-27 05:25:31
【问题描述】:

有这个结构:

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

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

该类型在新参数列表中不可用以选择其类型,并且无法通过浏览输入完全限定的类型名称,因为它没有找到。

大部分教程和副本都不清楚、不完整并且只提到类,很少提及结构。

【问题讨论】:

    标签: c# class struct app-config application-settings


    【解决方案1】:

    主要来自这篇文章: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;
      }
    }
    

    这里是类型转换器类:

    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>
    

    【讨论】:

      猜你喜欢
      • 2021-04-27
      • 2012-10-13
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多