2010-02-25 63 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