我有一個通過MEF使用DLL的可執行文件。我使用使用Configuration Manager.GetSection和Configuration Manager.OpenExe
var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
return appConfig.AppSettings.Settings["Version"].Value;
現在我想讓它這樣的DLL可以在DLL的配置文件即席項目成功加載每個DLL的配置文件的AppSettings的鑰匙。
因此,我已將此添加到配置文件
<configSections>
<section name="AdHocRules" type="BusinessRules.AdHocConfiguration, BusinessRules" />
</configSections>
<AdHocRules BaseRuleNumber="ConfigEx1" RuleName="Config Example" EffectiveDate="5/1/2010" Value="Example" IsValid="true"/>
而且我創建了一個類來讀取上面。當我在一個測試控制檯應用程序中運行這個不使用DLL時 - 所以一切都編譯在一起,一個應用程序配置一切工作正常
但我想使用DLL的配置文件,我不斷收到錯誤
無法轉換 類型 'System.Configuration.DefaultSection' 的對象鍵入 「BusinessRules.AdHocConfiguration
這不是工作; - 它拋出上述
var cm = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
AdHocConfiguration adhoc = (AdHocConfiguration)cm.GetSection("AdHocRules");
這是代碼 - 即席爲null,因爲它不是從正確的配置文件
AdHocConfiguration adhoc = (AdHocConfiguration)ConfigurationManager.GetSection("AdHocRules");
BusinessRules.Rule r = new BusinessRules.Rule();
r.BaseRuleNumber = adhoc.baserulenumber;
r.RuleName = adhoc.rulename;
r.EffectiveDate = adhoc.effectivedate;
r.Value = adhoc.value;
任何想法加載?