// WinConfiguration.cs
using System;
using System.Configuration;
namespace BANANA.Common
{
public class WinConfiguration : ConfigurationSection
{
public readonly static BANANA.Common.WinConfiguration Settings
= (BANANA.Common.WinConfiguration)System.Configuration.ConfigurationManager.GetSection("BANANA");
[ConfigurationProperty("connections")]
public ConnectionsCollection Connections
{
get { return (ConnectionsCollection)this["connections"]; }
}
[ConfigurationProperty("cryptography")]
public CryptographyCollection Cryptography
{
get { return (CryptographyCollection)this["cryptography"]; }
}
}
}
// app.config
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="BANANA" type="BANANA.Common.WinConfiguration"/>
</configSections>
<BANANA>
<connections>
<add name="SqlServer2008" connectionString="" providerName="System.Data.SqlClient" priority="100" />
</connections>
<cryptography>
<add type="DES" value="12345" />
<add type="TripleDES" value="12345" />
</cryptography>
</BANANA>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
它曾经完美地在 web.config 中工作。我只是更改了班级名称。这是我的 web.config 和自定义配置类。
// WebConfiguration.cs
using System;
using System.Configuration;
namespace BANANA.Common
{
public class WebConfiguration : ConfigurationSection
{
public readonly static BANANA.Common.WebConfiguration Settings = (BANANA.Common.WebConfiguration)System.Web.Configuration.WebConfigurationManager.GetSection("BANANA");
[ConfigurationProperty("connections")]
public ConnectionsCollection Connections
{
get { return (ConnectionsCollection)this["connections"]; }
}
[ConfigurationProperty("cryptography")]
public CryptographyCollection Cryptography
{
get { return (CryptographyCollection)this["cryptography"]; }
}
}
}
// web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="BANANA" type="BANANA.Common.WebConfiguration"/>
</configSections>
<BANANA>
<connections>
<add name="SqlServer2008" connectionString="" providerName="System.Data.SqlClient" priority="100" />
</connections>
<cryptography>
<add type="DES" value="12345" />
<add type="TripleDES" value="12345" />
</cryptography>
</BANANA>
</configuration>
它适用于 WebConfiguration.cs 和 web.config。但是对于 WinConfiguration.cs 和 app.config,它给了我一些错误,例如“'BANANA.Common.WinConfiguration' 的类型初始化程序引发了异常。”。谁能帮我解决这个错误?