gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:33:48 27 4
gpt4 key购买 nike

  • 我有 .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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com