.NET Framework 的版本相容性Version compatibility in the .NET Framework

回溯相容性表示針對特定平台版本開發的應用程式將會在該平台的較新版本上執行。Backward compatibility means that an app that was developed for a particular version of a platform will run on later versions of that platform. .NET Framework 嘗試最大化回溯相容性:針對某一個 .NET Framework 版本撰寫的原始程式碼應該在較新版本的 .NET Framework 上編譯,且在某一個 .NET Framework 版本上執行的二進位檔行為應該與較新版本 .NET Framework 相同。The .NET Framework tries to maximize backward compatibility: Source code written for one version of the .NET Framework should compile on later versions of the .NET Framework, and binaries that run on one version of the .NET Framework should behave identically on later versions of the .NET Framework.

應用程式的版本相容性Version compatibility for apps

根據預設,應用程式會在其建置所針對的 .NET Framework 版本上執行。By default, an app runs on the version of the .NET Framework that it was built for. 如果該版本不存在,且應用程式組態檔不會定義支援的版本,則可能會發生 .NET Framework 初始化錯誤。If that version isn't present and the app configuration file doesn't define supported versions, a .NET Framework initialization error may occur. 在此例中,嘗試執行應用程式的作業將會失敗。In this case, the attempt to run the app will fail.

若要定義應用程式執行所在的特定版本,請將一個或多個 <supportedRuntime> 項目新增至應用程式的組態檔。To define the specific versions on which your app runs, add one or more <supportedRuntime> elements to your app's configuration file. 每一個 <supportedRuntime> 項目都會列出支援的執行階段版本,最先指定的是最優先的版本,而最後指定的則是優先順序最低的版本。Each <supportedRuntime> element lists a supported version of the runtime, with the first specifying the most preferred version and the last specifying the least preferred version.

<configuration>
   <startup>
      <supportedRuntime version="v2.0.50727" />
      <supportedRuntime version="v4.0" />
   </startup>
</configuration>

如需詳細資訊,請參閱如何:設定應用程式以支援 .NET Framework 4 或 4.xFor more information, see How to: Configure an App to Support .NET Framework 4 or 4.x.

元件的版本相容性Version compatibility for components

應用程式可以控制其執行的 .NET Framework 版本,元件則不能。An app can control the version of the .NET Framework on which it runs, but a component can't. 元件和類別庫會載入至特定應用程式的內容中,這就是為什麼它們會自動執行應用程式所執行的 .NET Framework 版本。Components and class libraries are loaded in the context of a particular app, and that's why they automatically run on the version of the .NET Framework that the app runs on.

由於這項限制,所以相容性保證對於元件特別重要。Because of this restriction, compatibility guarantees are especially important for components. 從 .NET Framework 4 開始,您可以指定元件在多個版本中維持相容所需的程度,方法是將 System.Runtime.Versioning.ComponentGuaranteesAttribute 屬性套用至該元件。Starting with the .NET Framework 4, you can specify the degree to which a component is expected to remain compatible across multiple versions by applying the System.Runtime.Versioning.ComponentGuaranteesAttribute attribute to that component. 工具可以使用這個屬性來偵測將來的元件版本中,是否有可能違反相容性保證的狀況。Tools can use this attribute to detect potential violations of the compatibility guarantee in future versions of a component.

回溯相容性和 .NET FrameworkBackward compatibility and the .NET Framework

.NET Framework 4.5 和更新版本可以與使用舊版 .NET Framework 所建置的應用程式回溯相容。The .NET Framework 4.5 and later versions are backward-compatible with apps that were built with earlier versions of the .NET Framework. 換句話說,使用舊版所建置的應用程式和元件不需經過修改,就可在 .NET Framework 4.5 和更新版本上運作。In other words, apps and components built with previous versions will work without modification on the .NET Framework 4.5 and later versions. 不過,應用程式預設會在作為其開發目標的通用語言執行平台版本上執行,因此您可能需要提供組態檔,才能讓您的應用程式在 .NET Framework 4.5 或更新版本上執行。However, by default, apps run on the version of the common language runtime for which they were developed, so you may have to provide a configuration file to enable your app to run on the .NET Framework 4.5 or later versions. 如需詳細資訊,請參閱本文前面的應用程式的版本相容性一節。For more information, see the Version compatibility for apps section earlier in this article.

在實際操作中,.NET Framework 中似乎前後不一致的變更以及程式設計技術的變更可能會破壞此相容性。In practice, this compatibility can be broken by seemingly inconsequential changes in the .NET Framework and changes in programming techniques. 例如,.NET Framework 4.5 中的效能改良可能會暴露在舊版不會發生的競爭情況。For example, performance improvements in the .NET Framework 4.5 can expose a race condition that did not occur on earlier versions. 同樣地,使用 .NET Framework 組件的硬式編碼路徑、搭配特定版本的 .NET Framework 執行相等比較以及使用反映來取得私用欄位的值,都不是具有回溯相容性的作法。Similarly, using a hard-coded path to .NET Framework assemblies, performing an equality comparison with a particular version of the .NET Framework, and getting the value of a private field by using reflection are not backward-compatible practices. 此外,每一個 .NET Framework 版本都包含可能會影響某些應用程式與元件之相容性的 Bug 修正和安全性相關的變更。In addition, each version of the .NET Framework includes bug fixes and security-related changes that can affect the compatibility of some apps and components.

如果您的應用程式或元件在 .NET Framework 4.5 (包括其點版本,.NET Framework 4.5.1、4.5.2、4.6、4.6.1、4.6.2、4.7、4.7.1、4.7.2 或 4.8) 上無法如預期運作,請使用下列檢查清單:If your app or component doesn't work as expected on the .NET Framework 4.5 (including its point releases, the .NET Framework 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, or 4.8), use the following checklists:

相容性和並存執行Compatibility and side-by-side execution

如果您找不到適當的因應措施來解決問題,請記得 .NET Framework 4.5 (或其點版本其中之一) 與版本 1.1、2.0 和 3.5 並存執行,且是取代第 4 版的就地更新。If you can't find a suitable workaround for your issue, remember that the .NET Framework 4.5 (or one of its point releases) runs side by side with versions 1.1, 2.0, and 3.5, and is an in-place update that replaces version 4. 若是以 1.1、2.0 和 3.5 版為目標的應用程式,您可以在目標電腦上安裝適當的 .NET Framework 版本,以便在最佳環境中執行應用程式。For apps that target versions 1.1, 2.0, and 3.5, you can install the appropriate version of the .NET Framework on the target machine to run the app in its best environment. 如需並存執行的詳細資訊,請參閱並存執行For more information about side-by-side execution, see Side-by-Side Execution.

另請參閱See also