app.config中的自定义节在c#中引发错误
本文关键字:错误 config 自定义 app | 更新日期: 2023-09-27 18:26:52
//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'的类型初始值设定项引发了异常"。有人能帮我纠正这个错误吗?
我认为您必须指定配置类所在的程序集名称(EXE或DLL的名称):
<section name="BANANA" type="BANANA.Common.WinConfiguration, YOUR_ASSEMBLY_NAME"/>