C# app.config中的自定义节在c中引发错误#
//WinConfiguration.csC# app.config中的自定义节在c中引发错误#,c#,configuration,C#,Configuration,//WinConfiguration.cs using System; using System.Configuration; namespace BANANA.Common { public class WinConfiguration : ConfigurationSection { public readonly static BANANA.Common.WinConfiguration Settings = (BANANA.Com
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"]; }
}
}
}
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"]; }
}
}
}
//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
<?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。
但是对于winconfig.cs和app.config,它会给我一些错误,比如“BANANA.Common.winconfig”的类型初始值设定项引发了异常。”。
有人能帮我修复此错误吗?我认为您必须指定配置类所在的程序集名称(EXE或DLL的名称):
<section name="BANANA" type="BANANA.Common.WinConfiguration, YOUR_ASSEMBLY_NAME"/>
我认为您必须指定配置类所在的程序集名称(EXE或DLL的名称):
<section name="BANANA" type="BANANA.Common.WinConfiguration, YOUR_ASSEMBLY_NAME"/>
您在内部异常中得到了什么错误消息?@fcuesta内部异常是“创建香蕉配置节处理程序时出错:无法从程序集'System.configuration,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a'加载类型'BANANA.Common.WinConfiguration'。”您在内部异常中得到了什么错误消息?@fcuesta内部异常是“为香蕉创建配置节处理程序时出错:无法从程序集“System.configuration,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“BANANA.Common.WinConfiguration”。“哇……您解决了我的问题。”。为什么会这样?当我使用web.config.Wow时没有问题……你解决了我的问题。为什么会这样?使用web.config时没有问题。