2010-02-25 65 views
2

所以这对我来说是一个新的。如何定义配置段

我想在我的类库中定义一个ConfigurationSection类,该类从我的WinForms应用程序中的App.Config中抽取。我从来没有这样做过,但从下面的例子,这是我必须的。

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

<?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类。

using System.Configuration;

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

    } 
} 

所以任何机会,任何人都可以指出哪里我已经错了

谢谢!

回答

2

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

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

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

您也可以摆脱allowDefinition的和allowLocation标签......你已经把默认英寸

+0

我已经设置了线 <节名称=“ReportEngineConfig” TYPE =“Optima.ReportEngine .ReportEngineConfig,ReportEngine“allowDefinition =”Everywhere“allowLocation =”true“/> ReportEngine是dll的名称,但是这个[”value“]仍然得到空引用? – 2010-02-25 11:11:52