以框架为目标到底意味着什么?如何最大限度地提高兼容性?

发布于 2024-10-06 04:32:06 字数 666 浏览 9 评论 0原文

大家好,

自从我第一次开始用 C# 编码以来,这一直让我很困惑。我的目标是创建一个可以在用户拥有的最新 .NET 框架(无论是什么)上运行的程序集。我不想要求安装 .NET 4,但如果用户拥有它,我想使用它,更重要的是,如果用户 .NET,我希望它仍然可以工作4 以下没有任何内容。我开始怀疑这是不可能的。

当我更改 Visual Studio 中的“目标框架”时,我真的不明白我在选择什么。这是否意味着“将与此版本及更高版本兼容”?或者“仅与此版本兼容”?到目前为止似乎是后者;我在虚拟机中的测试显示,.NET 4 目标程序集在没有 .NET 4 的情况下失败,.NET 3.5 目标程序集在没有 .NET 3.5 的情况下失败。有没有办法设置这个以便我可以实现最大的兼容性?

更新:为了澄清,我有一个针对 .NET 2 的程序。这里的海报似乎表明它应该使用 .NET 4 加载。但是在只有 .NET 4 且之前没有任何内容的环境中,它无法加载。

UPDATE2:好的,弄清楚了,但它比这里的海报想象的要复杂得多。我正在提出一个不同的问题来讨论复杂性,但简短的版本是,如果您愿意,您需要一个包含 的 app.config在 .NET 4 上运行非 .NET 4 程序集。如果没有它,它们将无法加载。

Greetings all,

This has confused me ever since I first started coding in C#. My goal is to create an assembly that will run on the most recent .NET framework the user has, whatever that may be. I don't want to require .NET 4 to be installed, but I want to use it if the user has it, and more importantly, I want it to still work if the user has only .NET 4 and nothing below. I'm beginning to suspect this is not even possible.

I don't really understand what I'm selecting when I change the "Target Framework" in Visual Studio. Does that mean "will be compatible with this version and up"? Or "will be compatible with only this version"? It seems like it's the latter so far; my tests in virtual machines show .NET 4 targeted assemblies failing without .NET 4, and .NET 3.5 targeted assemblies failing without .NET 3.5. Is there no way to set this so I can achieve maximum compatibility?

UPDATE: To clarify, I have a program that targets .NET 2. The posters here seem to indicate that it should load with .NET 4. But in an environment with only .NET 4 and nothing before it, it fails to load.

UPDATE2: Okay, figured it out, but it's far more complex than the posters here seem to think. I'm opening a different question to discuss the intricacies, but the short version is, you need an app.config with <supportedRuntime version="v4.0" /> in it if you want to run non-.NET 4 assemblies on .NET 4. They won't load without it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

上课铃就是安魂曲 2024-10-13 04:32:06

这些框架被设计为向后兼容;如果您有一个用 .NET 2.0 编写的程序,则可以在 4.0 运行时中运行它,因为没有一个框架会删除先前版本所具有的功能(这就是为什么我们仍然拥有像 ArrayList 这样的非泛型集合,尽管它们已被弃用,取而代之的是通用集合)。然而,反之则不一定成立。 4.0 应用程序不保证在 2.0 中运行,因为它可能会利用新运行时的新功能,而这些功能在以前的版本中不可用。无论如何,如果您希望应用程序尝试在没有专门针对的运行时版本上运行,则必须在 app.config 中使用 SupportedRuntime 元素进行指定。

要回答您的具体问题,您可以通过以下基本过程执行您想要的操作:

  • 开发您的应用程序以针对您想要支持的最早的框架版本。这将禁止访问较新运行时的新功能(例如 3.5 中的 Linq 和 4.0 中的动态类型),确保您的应用程序不需要任何受支持的运行时无法提供的任何功能。

  • 使用 app.config 文件中的 SupportedRuntime 元素指定您的应用程序可接受的框架。这将告诉初始化应用程序将在其中运行的运行时的本机代码,如果找不到目标版本,则任何其他版本都是可以接受的。我认为行为是首先查找目标框架,如果不可用,则应使用最新支持的运行时。

The frameworks are designed to be backwards-compatible; if you have a program written in .NET 2.0, you can run it in the 4.0 runtime, because none of the frameworks ever remove functionality that a prior version had (which is why we still have the non-generic collections like ArrayList, even though they're deprecated in favor of generic collections). However, the reverse is not necessarily true; a 4.0 app is not guaranteed to run in 2.0, because it MAY take advantage of new features of the new runtime that are not available in prior versions. In any case, if you want your app to attempt to run on runtime versions it does not specifically target, you must specify that in the app.config using SupportedRuntime elements.

To answer your specific question, you can do what you want with the following basic procedure:

  • Develop your app to target the EARLIEST framework version that you want to support. This will disable access to newer features of newer runtimes (like Linq in 3.5, and dynamic typing in 4.0) ensuring your app will not require any feature that cannot be provided by any of the supported runtimes.

  • Specify the frameworks that are acceptable to your application by using the SupportedRuntime element in your app.config file. This will tell the native code that initializes the runtime in which your app will run that if it can't find the targeted version, any of the others are acceptable. I believe the behavior is to look for the targeted framework first, and if not available it should use the newest supported runtime.

じ违心 2024-10-13 04:32:06

.NET 是向后兼容的,这意味着如果您选择 .NET Framework 2.0 作为目标框架,它将在已安装的版本 2.0、3.0、3.5 和 4.0 上运行。
但是,如果您选择 4.0 版作为目标框架,则只有安装了 4.0 版它才会运行。

.NET is backward-compatible, this means if you select .NET Framework 2.0 as target framework, it will run on installed version 2.0, 3.0, 3.5 and 4.0.
But if you select e.g version 4.0 as target framework, it will only run if you have version 4.0 installed.

流星番茄 2024-10-13 04:32:06

定位的工作原理如下:如果您定位 .NET 3.5,则如果用户没有 .NET 3.5,它将不会运行。如果用户有任何更高版本,它将运行。

为了获得最大的兼容性,请选择.NET 2.0,它甚至可以在.NET 3.5、4.0等上运行。

但是,您可能会失去 .NET 的新功能,例如 LINQ 等。

http://msdn.microsoft.com/en-us/library/bb398197(VS.100).aspx

我仍然可以在 VisualStudio 2010 中以 .NET Framework 2.0 为目标吗?

Here is how targeting works: If you target .NET 3.5, it will not run if user does not have .NET 3.5. It will run if user has any higher version.

For maximum compatibility, choose .NET 2.0, it will run even on .NET 3.5, 4.0 and so on.

However, you might loose out on new features of .NET such as LINQ etc.

http://msdn.microsoft.com/en-us/library/bb398197(VS.100).aspx

Can I still target .NET Framework 2.0 in VisualStudio 2010?

愿得七秒忆 2024-10-13 04:32:06

我相信这应该是您选择的任何版本及更高版本。因此,如果您的目标是 2.0 框架,那么如果客户端安装了 2.0、3.0、3.5 或 4.0 框架,它应该可以工作。

当您选择此选项时,您还选择了语言功能,因此我知道您无法通过面向 2.0 框架的应用程序来使用 4.0 框架功能。即使动态加载程序集也不起作用,尝试加载从 2.0 应用程序编译为 4.0 的 dll,它不会让你这样做。

I believe this is supposed to be whatever version you select and upwards. So if you target 2.0 framework, it should work if client has 2.0, 3.0, 3.5, or 4.0 frameworks installed.

You are also selecting language features when you pick this, so I know of no way that you would be able to use 4.0 framework features using an application targeting 2.0 framework. Even dynamically loading assemblies doesn't work, try loading a dll compiled to 4.0 from a 2.0 application, it won't let you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文