我在昨天發佈的問題上取得了一些進展,因此我正在重寫該帖子。由於組件中缺少類型導致的例外(修訂)
我的問題似乎與我使用泛型有關。下面是App.config中的相關部分(帶有空格的可讀性格式):
<configSections>
<section
name="NA5300ResolverSynchroDevices"
type="InfrastructureModule.DeviceConfiguration.DeviceConfigurationSection
<NA5300ResolverSynchroModule.NA5300ResolverSynchroConfigurationElement>,
NA5300ResolverSynchroModule">
</section>
</configSections>
<NA5300ResolverSynchroDevices>
<Device deviceName="AzResolverSynchro" busAddress="7"/>
<Device deviceName="ElResolverSynchro" busAddress="8"/>
</NA5300ResolverSynchroDevices>
以下是我正在試圖映射到配置節的類:
namespace InfrastructureModule.DeviceConfiguration
{
public class DeviceConfigurationSection<T> : ConfigurationSection
where T : DeviceConfigurationElement, new()
{
[ConfigurationProperty("", IsDefaultCollection = true, IsKey = false)]
public DeviceConfigurationElementCollection<T> Devices
{
get { return (DeviceConfigurationElementCollection<T>) base[""]; }
set { base[""] = value; }
}
}
}
下面是嘗試的C#代碼訪問配置文件:
DeviceConfigurationSection<NA5300ResolverSynchroConfigurationElement> devices =
ConfigurationManager.GetSection("NA5300ResolverSynchroDevices") as
DeviceConfigurationSection<NA5300ResolverSynchroConfigurationElement>;
這裏的異常文本我越來越:
出錯創建NA5300ResolverSynchroDevices配置節處理程序:無法從程序集「NA5300ResolverSynchroModule」加載類型「InfrastructureModule.DeviceConfiguration.DeviceConfigurationSection <NA5300ResolverSynchroModule.NA5300ResolverSynchroConfigurationElement>」。
我知道,在C#泛型是在運行時,而不是在編譯時(不像C++)實例化。我還不太瞭解泛型,以瞭解當泛型類型和實例化類型存在於不同的程序集中時,運行時生成的類型被認爲存在於哪個程序集中。上面,我告訴運行時在彙編NA5300ResolverSynchroModule中查找它。我也試着告訴它在AssemblyModule程序集中尋找它。兩者都不起作用。
我正在嘗試使用genric類型,因爲我將有許多配置部分的相應ConfigurationSection派生類型都將具有上面顯示的形式。我想避免代碼重複。
任何人都可以看到爲什麼我的方法失敗,我該如何解決它?
這是由於它的創建配置處理的方式。配置部分是什麼樣的? – 2009-08-17 23:53:08
如果類型不在程序集中,則會導致此異常。 .NET不是在發明這個,或是對你說謊。它確實需要這種類型,它確實不在該組件中。你需要調查以瞭解這可能是真實的。 – 2009-08-17 23:56:11