【问题标题】:Store array of custom class in settings file将自定义类的数组存储在设置文件中
【发布时间】:2013-07-30 03:45:24
【问题描述】:

我有一门课:

public class CustomClass
{
    public string Columns;
    public string Filter;

    public string SourceDB;
    public string SourceTable;

    public string DestinationDB;
    public string DestinationTable;
}

在用户设置中,我需要存储一个CustomClass数组。这是因为我需要用户能够在 app.config 文件中指定多个 CustomClass。

【问题讨论】:

标签: c# arrays class app-config setting


【解决方案1】:

您可以在 App.config 中声明 CustomClassSection,并在 CustomClass 实例的集合中声明。比如:

<configuration>
    <configSections>
        <section name="CustomClassSection" type = "A type of class section " />
    </configSections>
</configuration>

<CustomClassSection>
    <CustomClass Columns="column1" Filter="filter1" SourceDB="sourcedb1" SourceTable="sourcetable1" DestinationDB="destdb1" DestinationTable="desttable1"/>
    <CustomClass Columns="column2" Filter="filter2" SourceDB="sourcedb2" SourceTable="sourcetable2" DestinationDB="destdb2" DestinationTable="desttable2"/>
...
</CustomClassSection>

您可以在此处查看如何使用部分:How to create custom config section in app.config?

【讨论】:

    【解决方案2】:

    您必须首先在项目设置文件中创建一个设置,我们将其命名为 CustomClasses。下一部分有点棘手,因为它涉及编辑 Settings.settings 文件的 XML:

    <?xml version='1.0' encoding='utf-8'?>
    <SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" 
                  CurrentProfile="(Default)" 
                  GeneratedClassNamespace="ConsoleApplication1.Properties" 
                  GeneratedClassName="Settings">
      <Profiles />
      <Settings>
        <Setting Name="CustomClasses" 
                 GenerateDefaultValueInCode="false" 
                 Type="System.Collections.Generic.List&lt;ConsoleApplication1.CustomClass&gt;" 
                 Scope="User">
        </Setting>
      </Settings>
    </SettingsFile>
    

    如果您打开 Settings.Designer.cs 文件,您现在应该有:

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public global::System.Collections.Generic
        .List<ConsoleApplication1.CustomClass> CustomClasses {
        get {
            return ((global::System.Collections.Generic
                .List<ConsoleApplication1.CustomClass>)(this["CustomClasses"]));
        }
        set {
            this["CustomClasses"] = value;
        }
    }
    

    您可以将设置保存在您的应用程序中:

    class Program
    {
        static void Main(string[] args)
        {
            Properties.Settings.Default.CustomClasses = new List<CustomClass>() {
                new CustomClass(){Columns="columns1"},
                new CustomClass(){Columns="columns2"},
                new CustomClass(){Columns="columns3"},
                new CustomClass(){Columns="columns4"}
            };
            Properties.Settings.Default.Save();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-26
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多