0

该项目有一个名为 MyNamespace.MyConfig 的自定义配置节类。该类存储在 C# 库 MyNamespace.MyAssembly.dll 中。

该项目还有一个控制台应用程序。客户端希望它以与 dll 相同的方式被调用,即 MyNamespace.MyAssembly.exe。

App.config 文件具有以下定义:

 <section name="myConfigSection" type="MyNamespace.MyConfig, MyNamespace.MyAssembly"/>

现在的问题是应用程序没有找到 MyConfig 类,因为它正在寻找错误的程序集 - 在 exe 文件中。

第一个想法是更具体地了解程序集的名称,因此 ConfigurationManager 知道它应该查看 dll 而不是 exe:

 <section name="myConfigSection" type="MyNamespace.MyConfig, MyNamespace.MyAssembly.dll"/>

不幸的是,这没有帮助,应用程序说,它找不到名为 MyNamespace.MyAssembly.dll 的程序集。

使其工作的唯一方法是将应用程序程序集重命名为 MyNamespace.MyAssembly.Console,但客户端不同意这一点。

如何告诉 ConfigurationManager 它应该在 MyNamespace.MyAssembly.dll 而不是 MyNamespace.MyAssembly.exe 中查找我的 MyNamespace.MyConfig 类?

4

1 回答 1

1

不幸的是,在这种情况下,无法指示 .NET 首先搜索 .dll。即使有一个像这样的程序集的全名

<section name="myConfigSection" type="MyNamespace.MyConfig, MyNamespace.MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=...."/>

它仍然会首先检查当前的 exe,因为它们共享相同的程序集名称。我建议在不更改项目名称的情况下为您的配置项目使用不同的输出程序集名称。

在此处输入图像描述

然后使用

<section name="myConfigSection" type="MyNamespace.MyConfig, MyNamespace.MyAssemblyConfig />

在 app.config 中。

于 2012-05-10T18:51:01.653 回答