2

所以这对我来说是一个新的。

我正在尝试在我的类库中定义一个 ConfigurationSection 类,该类从我的 WinForms 应用程序中的 App.Config 中提取。我以前从未这样做过,但从下面的例子来看,这就是我必须要做的。

我的 WinForms 应用程序中的 app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="ReportEngineConfig" type="Optima.ReportEngine.ReportEngineConfig" allowDefinition="Everywhere" allowLocation="true"/>
  </configSections>

  <ReportEngineConfig>
    <ReportObjectVariableRegEx value="test" ></ReportObjectVariableRegEx>
  </ReportEngineConfig>
</configuration>

还有我的单独类库中的 ConfigurationSection 类。

使用 System.Configuration;

namespace Optima.ReportEngine
{
    public class ReportEngineConfig : ConfigurationSection
    {
        [ConfigurationProperty("ReportObjectVariableRegEx")]
        public string ReportObjectVariableRegEx
        {
            get
            {
                return (string)this["value"];
            }
        }

    }
}

所以任何人都可以指出我哪里出错了

谢谢!

4

1 回答 1

3

您的类型标记需要引用程序集名称,而不仅仅是类型名称:

type="Optima.ReportEngine.ReportEngineConfig, Optima.ReportEngineAssembly"

其中逗号后面的部分是包含 ReportEngineConfig 的程序集的名称。您还必须确保使用此 app.config 的应用程序引用了包含 ReportEngineConfig 的相同程序集。

你也可以去掉allowDefinition和allowLocation标签……你已经把默认值放进去了。

于 2010-02-25T11:04:02.917 回答