【问题标题】:Hosting CLR and programmatically providing app.config?托管 CLR 并以编程方式提供 app.config?
【发布时间】:2012-04-11 15:21:07
【问题描述】:

我有一个本地第三方游戏,它允许我通过在本地 DLL 中导出特定函数来为其编写本地扩展。我一直在使用它来使用 C++/CLI 编译器生成的托管代码来托管 CLR,从而允许我从游戏中调用 C# 代码。这一直很好,这是一个非常优雅的解决方案。

游戏加载 CLR 后,它会在游戏可执行文件夹中搜索 game.exe.config,以获取更多 .NET 程序集探测信息:

<?xml version="1.0"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="@Arma2NET"/>
    </assemblyBinding>
  </runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup></configuration>

这个任意配置文件破坏了我的文件夹结构;我希望我的项目在@Arma2NET 文件夹中是独立的,并且在游戏文件夹中没有随机配置文件。目前,由于游戏的文件夹要求,将项目文件与本地 CLR 托管 DLL 放在同一文件夹中是不可能的。

有没有办法在启动时通过本机代码以编程方式向 CLR 提供此配置,而不是自己编写整个 CLR 托管代码?

【问题讨论】:

    标签: interop c++-cli clr app-config com-interop


    【解决方案1】:

    我最终使用了 AssemblyResolve 事件来告诉 .NET 在哪里可以找到我的其余程序集:

        Assembly^ ResolveAssembly(Object ^sender, ResolveEventArgs ^e)
        {
            String ^directory = Path::GetDirectoryName(Assembly::GetExecutingAssembly()->Location);
            AssemblyName ^assemblyName = gcnew AssemblyName(e->Name);
            for each (String ^extension in gcnew array<String^> { ".dll", ".exe" })
            {
                String ^fileName = Path::Combine(directory, assemblyName->Name + extension);
                try
                {
                    return Assembly::LoadFile(fileName);
                }
                catch (...)
                {
                }
            }
            return nullptr;
        }
    

    【讨论】:

      【解决方案2】:

      您如何将库项目构建操作中的 app.config 更改为“嵌入式资源”,然后使用 ConfigurationManager.OpenMappedExeConfiguration 之类的东西来读取它。

      【讨论】:

      • 我不认为我可以这样做,因为 CLR 将是读取配置的人,而不是我的程序。
      猜你喜欢
      • 2011-12-15
      • 1970-01-01
      • 2010-09-08
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      • 2019-02-07
      相关资源
      最近更新 更多