在转换使用SlimDX的项目时,因此有非托管代码,到.NET 4.0,我遇到了以下错误:

混合模式程序集是根据运行时版本“v2.0.50727”构建的,如果没有额外的配置信息,则无法在4.0运行时中加载。

谷歌给了我解决方案,这是添加到应用程序配置:

<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>

我的问题是,useLegacyV2RuntimeActivationPolicy在做什么?我找不到任何相关文件。


当前回答

下面是我最近写的一篇解释,以帮助解决这个属性的信息空白。 http://www.marklio.com/marklio/PermaLink,guid,ecc34c3c-be44-4422-86b7-900900e451f9.aspx (Internet Archive Wayback Machine链接)

引用最相关的片段:

[Installing .NET] v4 is “non-impactful”. It should not change the behavior of existing components when installed. The useLegacyV2RuntimeActivationPolicy attribute basically lets you say, “I have some dependencies on the legacy shim APIs. Please make them work the way they used to with respect to the chosen runtime.” Why don’t we make this the default behavior? You might argue that this behavior is more compatible, and makes porting code from previous versions much easier. If you’ll recall, this can’t be the default behavior because it would make installation of v4 impactful, which can break existing apps installed on your machine.

这篇完整的文章对此进行了更详细的解释。在RTM,关于这方面的MSDN文档应该会更好。

其他回答

经过一段时间(和更多的搜索),我发现了Jomo Fisher的这篇博客。

One of the recent problems we’ve seen is that, because of the support for side-by-side runtimes, .NET 4.0 has changed the way that it binds to older mixed-mode assemblies. These assemblies are, for example, those that are compiled from C++\CLI. Currently available DirectX assemblies are mixed mode. If you see a message like this then you know you have run into the issue: Mixed mode assembly is built against version 'v1.1.4322' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information. [Snip] The good news for applications is that you have the option of falling back to .NET 2.0 era binding for these assemblies by setting an app.config flag like so: <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0"/> </startup>

所以看起来运行时加载混合模式程序集的方式已经改变了。我找不到关于这个改变的任何细节,也找不到为什么要这么做。但是useLegacyV2RuntimeActivationPolicy属性返回到CLR 2.0加载。

下面是我最近写的一篇解释,以帮助解决这个属性的信息空白。 http://www.marklio.com/marklio/PermaLink,guid,ecc34c3c-be44-4422-86b7-900900e451f9.aspx (Internet Archive Wayback Machine链接)

引用最相关的片段:

[Installing .NET] v4 is “non-impactful”. It should not change the behavior of existing components when installed. The useLegacyV2RuntimeActivationPolicy attribute basically lets you say, “I have some dependencies on the legacy shim APIs. Please make them work the way they used to with respect to the chosen runtime.” Why don’t we make this the default behavior? You might argue that this behavior is more compatible, and makes porting code from previous versions much easier. If you’ll recall, this can’t be the default behavior because it would make installation of v4 impactful, which can break existing apps installed on your machine.

这篇完整的文章对此进行了更详细的解释。在RTM,关于这方面的MSDN文档应该会更好。