我可以通过编程方式设置 app.config 'useLegacyV2RuntimeActivationPolicy' 属性吗?

问题描述 投票:0回答:2

我必须将 .NET 3.5 迁移到 4.0,但某些 dll 未加载,在谷歌搜索后我发现创建一个 app.config 可以解决它:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" />
  </startup>
</configuration>

我想在不使用配置文件的情况下设置这些选项,可以吗?

c# .net wpf configuration app-config
2个回答
6
投票

可以在运行时从 .NET 应用程序或库中进行设置,但有一些限制。您可以使用

ICLRRuntimeInfo
接口来处理此问题。详情请看我写的这篇文章


1
投票
技术上是可行的。您必须自己托管 CLR,以便在创建主 AppDomain 之前调用

ICLRRuntimeInfo::BindAsLegacyV2Runtime() 方法。 .config 文件通常是一个低得多的痛点,除非您已经在托管。

© www.soinside.com 2019 - 2024. All rights reserved.