如何从app.config读取对象?

ran*_*oon 6 c# configurationmanager app-config

这不是一个新问题,但我已经研究了两天,我能找到的所有答案都已过时或无益.我想要做的是将一个对象放入App.config,然后在程序启动时加载它.

我有一个名为"Person"的基本类,有三个autoproperties :(字符串)FirstName,(字符串)LastName和(int)Age.这是我的App.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/></startup>
  <configSections>
    <sectionGroup name="People">
      <section 
        name="Person" 
        type="Person.Person"
      />
    </sectionGroup>
  </configSections>
  <People>
    <Person>
      <Person type="Person.Person, Person">
        <FirstName>Jimmy</FirstName>
        <LastName>Dean</LastName>
        <Age>2</Age>
      </Person>
    </Person>
  </People>
</configuration>
Run Code Online (Sandbox Code Playgroud)

这是我的计划:

using System;
using System.Configuration;

namespace AppConfigTest
{
    class AppConfigTester
    {
        public static void Main(string[] args)
        {
            var guy = (Person.Person) ConfigurationManager.GetSection("People/Person");
            Console.WriteLine(guy.FirstName);
            Console.WriteLine(guy.LastName);
            Console.WriteLine(guy.Age);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

目前它与ConfigurationErrorsException崩溃.任何帮助将非常感激.令人难以置信的是,当App.config应该让这种事情变得更容易时,这是非常困难的.

小智 8

鉴于Person POCO课程:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

首先,您需要创建一个继承System.Configuration.ConfigurationElement的类,如下所示:

public class PersonElement : ConfigurationElement
{
    public string InnerText { get; private set; }

    protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
    {
        InnerText = reader.ReadElementContentAsString();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是必需的,因此您可以<FirstName>Jimmy</FirstName>使用内部文本等配置元素.

接下来,您需要一个继承System.Configuration.ConfigurationSection的类,如下所示:

public class PersonSection : ConfigurationSection
{
    [ConfigurationProperty("FirstName")]
    public PersonElement FirstName
    {
        get { return this["FirstName"] as PersonElement; }
        set { this["FirstName"] = value; }
    }

    [ConfigurationProperty("LastName")]
    public PersonElement LastName
    {
        get { return this["LastName"] as PersonElement; }
        set { this["LastName"] = value; }
    }

    [ConfigurationProperty("Age")]
    public PersonElement Age
    {
        get { return this["Age"] as PersonElement; }
        set { this["Age"] = value; }
    }

    public Person CreatePersonFromConfig()
    {
        return new Person()
        {
            FirstName = this.FirstName.InnerText,
            LastName = this.LastName.InnerText,
            Age = Convert.ToInt32(this.Age.InnerText)
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

您的app.config应如下所示:

<configuration>
    <configSections>
        <sectionGroup name="People">
            <section name="Person" type="Example.PersonSection, Example" />
        </sectionGroup>
    </configSections>
    <People>
        <Person>
            <FirstName>Jimmy</FirstName>
            <LastName>Dean</LastName>
            <Age>2</Age>
        </Person>
    </People>
</configuration>
Run Code Online (Sandbox Code Playgroud)

最后在代码中的某处执行此操作:

PersonSection config = (PersonSection)ConfigurationManager.GetSection("People/Person");
Person guy = config.CreatePersonFromConfig();
Run Code Online (Sandbox Code Playgroud)

  • 哇谢谢你!我不知道我需要PersonElement类.这个学习曲线对我来说非常陡峭,所以我非常感谢你的帮助. (2认同)