使用 .net 4.0 编写的程序,但我需要该应用程序在运行 .net 2.0 的计算机上运行。

3
这个问题从标题就很清楚了,但是让我详细解释一下。我刚刚完成了我的应用程序,我相信它是在.Net 4.0框架下编写的(见下图)。意识到需要使应用程序与甚至最早版本的.Net框架兼容后,我回去修改了Visual Studio属性窗口中的目标框架。不幸的是,这样做留下了一个痛苦的长列表警告和一个关键错误。我想知道是否有人有解决方案,可以让我编写的使用 .Net 4.0 框架的应用程序在运行 .Net 2.0 框架的环境中运行。

原始编译

证明已编译为 .Net 4.0 框架

错误列表:

.Net 2.0 错误 谢谢, Evan

编辑:

我知道对于我们大多数人来说第二张图片不完全可见,所以我会在下面列出错误:

Warning 9   The referenced component 'System.Xml.Linq' could not be found.  
Warning 10  The referenced component 'System.Data.DataSetExtensions' could not be found.    
Warning 8   The referenced component 'System.Core' could not be found.  
Warning 11  The referenced component 'Microsoft.CSharp' could not be found.     
Warning 4   The primary reference "System.Xml.Linq", which is a framework assembly, could not be resolved in the currently targeted framework. ".NETFramework,Version=v2.0". To resolve this problem, either remove the reference "System.Xml.Linq" or retarget your application to a framework version which contains "System.Xml.Linq".   Protection
Warning 2   The primary reference "System.Xml.Linq" could not be resolved because it has an indirect dependency on the framework assembly "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v2.0". To resolve this problem, either remove the reference "System.Xml.Linq" or retarget your application to a framework version which contains "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".   Protection
Warning 5   The primary reference "System.Data.DataSetExtensions", which is a framework assembly, could not be resolved in the currently targeted framework. ".NETFramework,Version=v2.0". To resolve this problem, either remove the reference "System.Data.DataSetExtensions" or retarget your application to a framework version which contains "System.Data.DataSetExtensions". Protection
Warning 3   The primary reference "System.Data.DataSetExtensions" could not be resolved because it has an indirect dependency on the framework assembly "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v2.0". To resolve this problem, either remove the reference "System.Data.DataSetExtensions" or retarget your application to a framework version which contains "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".   Protection
Warning 1   The primary reference "System.Core", which is a framework assembly, could not be resolved in the currently targeted framework. ".NETFramework,Version=v2.0". To resolve this problem, either remove the reference "System.Core" or retarget your application to a framework version which contains "System.Core".   Protection
Warning 6   The primary reference "Microsoft.CSharp", which is a framework assembly, could not be resolved in the currently targeted framework. ".NETFramework,Version=v2.0". To resolve this problem, either remove the reference "Microsoft.CSharp" or retarget your application to a framework version which contains "Microsoft.CSharp".    Protection
Error   7   Could not load file or assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Line 123, position 5.    C:\Users\FARINA_EVAN\Documents\Programming\C#\Run Time Crypter\Run Time Crypter\Properties\Resources.resx   123 5   Protection

1
通过重新编码以排除任何4.0特定功能。唉,我认为这里没有捷径。 - Robert Harvey
如果您右键单击并选择查看图像,则图像实际上非常清晰可见。 - DeCaf
4个回答

3
当您创建一个针对.NET 4.0的新项目时,Visual Studio会自动添加对一些常用的.NET 4.0程序集的引用。然而,在更改目标平台时,这些引用并不会被删除,因此需要手动删除,因为您不能从.NET 2.0应用程序中引用.NET 4.0程序集。
首先删除这些错误的引用,查看是否仍有错误。如果您使用了.NET 2.0不可用的任何.NET特性,则可能需要更改一些源代码,或者如果缺少.NET 2.0程序集,则需要添加新的引用。
任何出现的编译器错误都有可能指向正确的方向。

2

是的,但您需要删除任何引用大于2的.net版本的代码。为了消除上述错误,您需要删除所有项目中显示“找不到引用组件xx”的引用。此外,您需要搜索资源、配置等文件,将任何对.net 4的引用替换为.net 2-从4.0.0.0到2.0.0.0,如果在.net 2中不存在,则完全删除这些引用。


那么,当我重新添加时,Visual Studio会自动查找每个dll的.NET 2.0版本吗? - user725913
@Evan:其中一些DLL没有.NET 2.0的等效版本。 - Robert Harvey

2
错误信息告诉你出了什么问题,比如说在你的项目中有一些引用无法解析。展开项目树中的引用节点,并删除带有警告图标的引用。
如果你尝试编译后,可能会得到其他关于类、命名空间或关键字的错误,这些在.NET 2.0中不可用。你需要删除这些或者修改你的代码。

我知道问题出在哪里,恕我直言。但我不知道最好的解决方法。我不想在我的应用程序中搞砸任何东西。 - user725913

0

由于 .NET 2.0 中没有 Linq,您可以尝试使用 LinqBridge 作为替代方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接