.NET 4.0代码可以与.NET 2.0代码互操作吗? [英] Can .NET 4.0 code interoperate with .NET 2.0 code?

查看:85
本文介绍了.NET 4.0代码可以与.NET 2.0代码互操作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 2.0程序集中的.NET 4.0程序集调用代码是否存在兼容性障碍?反之亦然?



更具体地说,我正在研究使用基于.NET 2.0的第三方应用程序时对Visual Studio 2010的升级。该应用程序可以通过引用我的自定义代码的钩子进行扩展。反之亦然,我的代码将引用应用程序的程序集。

解决方案

.NET 4中的CLR可以使用.NET 2组装并正确使用它们。



如果您希望.NET 2应用程序能够加载.NET 4程序集,则需要进行不同的配置。通过将requiredRuntime设置为.NET 4和旧版加载策略,您应该能够使用CLR 4强制.NET 2应用程序加载,这将允许使用.NET 4程序集。



设置您的app.config文件以包括:

 <?xml version = 1.0 ?> 
< configuration>
< startup useLegacyV2RuntimeActivationPolicy = true>
< supportedRuntime版本= v4.0 sku =。NETFramework,Version = v4.0 />
< / startup>
< / configuration>就是说,在这种情况下,我建议只使用VS 2010和targeting。 NET 3.5而不是.NET4。这将为CLR 2编译程序集,并完全避免此问题。


Are there compatibility barriers with a .NET 4.0 assembly calling code in a .NET 2.0 assembly? And vice versa?

More specifically, I'm investigating an upgrade to Visual Studio 2010 when using a third party application based on .NET 2.0. The application is extensible by hooks that reference my custom code. And vice versa, my code will reference the application's assemblies.

解决方案

The CLR, in .NET 4, can consume .NET 2 assemblies and use them properly.

If you want your .NET 2 application to be able to load .NET 4 assemblies, you'll need to configure it differently. By setting the requiredRuntime to .NET 4, and the legacy load policy, you should be able to force the .NET 2 application to load using CLR 4, which would allow your .NET 4 assemblies to be used.

Setup your app.config file to include:

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

That being said, in a situation like this, I'd recommend just using VS 2010 and targetting .NET 3.5 instead of .NET 4. This would compile your assemblies for CLR 2, and avoid this issue entirely.

这篇关于.NET 4.0代码可以与.NET 2.0代码互操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆