c# - ComVisible .NET 程序集和 app.config

标签 c# .net-4.0 app-config comvisible

  • 我有 .NET 程序集,其中一些类标记为 ComVisible
  • 此程序集已注册到 regasm/codebase "assembly_path"
  • 我有 app.config 名称(实际上是 - MyAssemblyName.dll.config),它位于程序集的文件夹中
  • 我通过 ConfigurationManager.AppSettings["SettingName"] 访问程序集中的 appSettings
  • 我有 VBScript 文件,它通过 CreateObject("...")
  • 创建我的 COM 对象
  • 创建对象时(从 VBScript),ConfigurationManager.AppSettings["SettingName"] 返回 null。看起来程序集没有看到配置文件。

我应该怎么做才能使其可行?

最佳答案

正如 Komyg 所说,一种可能的方法是直接读取配置文件,而不是使用 ConfigurationManager 的嵌入式行为。对于那些会遇到同样问题的人:而不是

ConfigurationManager.AppSettings["SettingName"]

你可以使用:

var _setting = ConfigurationManager.AppSettings["SettingName"];
// If we didn't find setting, try to load it from current dll's config file
if (string.IsNullOrEmpty(_setting))
{
    var filename = Assembly.GetExecutingAssembly().Location;
    var configuration = ConfigurationManager.OpenExeConfiguration(filename);
    if (configuration != null)
        _setting = configuration.AppSettings.Settings["SettingName"].Value;
}

这样您将始终使用位于程序集文件夹中的文件 YourAssemblyName.dll.config 中的读取设置。它还允许您使用 app.config 的其他功能(如 appSettingfile 属性),如果您将使用 XPath 或类似的东西。

关于c# - ComVisible .NET 程序集和 app.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8656317/

相关文章:

c# - 包含集合的自定义配置部分

c# - 如何使用 DI 通过 IValidatableObject 接口(interface)向实体提供验证器?

c# - 串成数组

c# - ViewModel 上的 ObservableObject 或 INotifyPropertyChanged

c# - 从任务工厂调度的线程更新 WPF 中的 UI 的正确方法是什么?

c# - 如何检查所有任务是否已正确完成?

c# - 如何将 'quick steps' 样式控件添加到Outlook 2010 功能区?

c# - 不同 MVC 版本的不同 NuGet Web.Config.Transforms

sqlite - SQLite 的 SubSonic 连接字符串

c# - 如何更好地将配置元素映射到应用程序对象?