通过反射和访问配置问题装载组件 [英] Problem loading assembly via Reflection and accessing configuration

查看:123
本文介绍了通过反射和访问配置问题装载组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加载程序集使用反射(使用 Assembly.LoadFrom 法),该组件中实例化某些类型。

I'm trying to load a .NET assembly with Reflection (using the Assembly.LoadFrom method), and instantiate some types within this assembly.

似乎这一切都工作正常,但一类,我试图实例访问它的类型初始值设定范围内的组件的配置:它 ConfigurationManager.GetSection(sectionName)。然后将组件抛出一个异常,因为配置部分无法找到。

This all seems to work fine, but one type I'm trying to instantiate accesses the assembly's configuration within its type initializer: it does ConfigurationManager.GetSection(sectionName). The assembly is then throwing an exception because the configuration section cannot be found.

对于我加载的程序集的配置文件是在同一目录中,并具有标准名称(即 AssemblyName.dll.config ),并配置肯定已被请求的部分。为什么不能在配置部分中找到?有没有我需要做的加载程序集时额外的东西?

The config file for the assembly I'm loading is in the same directory, and has the standard name (i.e. AssemblyName.dll.config), and the config definitely has the section being requested. Why can't the configuration section be found? Is there something extra I need to do when loading the assembly?

推荐答案

由于被读取配置文件的托管可执行文件的配置文件。因此,例如,如果你是从 foo.exe的您的配置文件应该被命名为运行你code Foo.exe.config 。该 AssemblyName.dll.config 从未使用过。如果你在一个网站上运行,那么你应该使用的web.config

Because the configuration file being read is the configuration file of the hosting executable. So for example if you are running you code from Foo.exe your config file should be named Foo.exe.config. The AssemblyName.dll.config is never used. If you are running this in a web site then you should use web.config.

您可以尝试使用<一个href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedexeconfiguration.aspx"相对=nofollow> OpenMappedExeConfiguration 方式:

var configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "AssemblyName.dll.config";
var section = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None).GetSection(sectionName);

这篇关于通过反射和访问配置问题装载组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆