app.config中的自定义部分在C#中引发错误 [英] custom section in app.config throws error in c#

查看:105
本文介绍了app.config中的自定义部分在C#中引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// WinConfiguration.cs

// 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

// 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和自定义配置类。

It used to work in web.config perfectly. I just changed the class name. Here is my web.config and custom configuration class.

// WebConfiguration.cs

// 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

// 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的类型初始化程序抛出异常。
谁能为我解决此错误吗?

It just works fine with WebConfiguration.cs and web.config. But with WinConfiguration.cs and app.config, it gives me some error like "The type initializer for 'BANANA.Common.WinConfiguration' threw an exception.". Can anyone fix this error for me?

推荐答案

我认为您必须指定程序集名称(名称配置类所在的EXE或DLL):

I think that you have to specify the assembly name (the name of the EXE or DLL) where the configuration class resides:

<section name="BANANA" type="BANANA.Common.WinConfiguration, YOUR_ASSEMBLY_NAME"/>

这篇关于app.config中的自定义部分在C#中引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆