这不是一个新问题,但我一直在研究这两天,所有我能找到的答案都是过时的或无益的。我想要做的是将一个对象放入App.config中,然后在程序启动时加载它。 (字符串)名字,(字符串)姓氏和(int)年龄。我有一个基本的类称为“人”与三个自动属性:(字符串)名字,(字符串)姓和(int)年龄。这是我的App.config文件:如何从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>
这里是我的程序:
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);
}
}
}
在其与ConfigurationErrorsException崩溃的时刻。任何帮助将非常感激。当App.config应该让这样的事情变得更简单时,我觉得这太难了。
你在哪里定义自定义配置部分? https://msdn.microsoft.com/en-us/library/2tw134k3.aspx – David
@David这是我错过了什么?我之前有一个,基于弃用的ConfigurationSettings,最终取消了它。在这一点上我很困惑。我会尽力做一个新的。 – randomraccoon