//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"]; }
}
}
}
//应用配置
<?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 中完美运行。 我刚刚更改了类(class)名称。 这是我的 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"]; }
}
}
}
//网络配置
<?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' 的类型初始值设定项引发了异常。”。 谁能帮我解决这个错误?
请您参考如下方法:
我认为你必须指定配置类所在的程序集名称(EXE 或 DLL 的名称):
<section name="BANANA" type="BANANA.Common.WinConfiguration, YOUR_ASSEMBLY_NAME"/>
