.NET Framework 中的應用程式相容性Application compatibility in the .NET Framework

相容性是每個 .NET 版本的重要目標。Compatibility is an important goal of each .NET release. 相容性可確保每個版本都是附加的,因此舊版會繼續工作。Compatibility ensures that each version is additive, so previous versions will continue to work. 另一方面,對先前的功能所做的變更(例如,為了改善效能、解決安全性問題或修正錯誤)可能會導致現有程式碼中的相容性問題,或在更新版本下執行的現有應用程式。On the other hand, changes to previous functionality (for example, to improve performance, address security issues, or fix bugs) can cause compatibility problems in existing code or existing applications that run under a later version.

每個應用程式都是以特定版本的 .NET Framework 為目標,如下所示:Each app targets a specific version of the .NET Framework by:

  • 在 Visual Studio 中定義目標架構。Defining a target framework in Visual Studio.
  • 在專案檔中指定目標架構。Specifying the target framework in a project file.
  • TargetFrameworkAttribute 套用至原始程式碼。Applying a TargetFrameworkAttribute to the source code.

從 .NET Framework 的某個版本遷移至另一個版本時,需要考慮兩種類型的變更:When migrating from one version of the .NET Framework to another, there are two types of changes to consider:

執行階段變更Runtime changes

執行時間問題是在電腦上放置新的執行時間,以及應用程式的行為變更時所發生的問題。Runtime issues are those that arise when a new runtime is placed on a machine and an app's behavior changes. 當在比目標較新的版本上執行時,.NET Framework 會使用quirked行為來模擬較舊的目標版本。When running on a newer version than what was targeted, the .NET Framework uses quirked behavior to mimic the older targeted version. 應用程式會在較新的版本上執行,但會如同在較舊的版本上執行一樣。The app runs on the newer version but acts as if it's running on the older version. .NET Framework 版本間的許多相容性問題是透過這種古怪的模型而降低。Many of the compatibility issues between versions of the .NET Framework are mitigated through this quirking model. 例如,如果二進位檔是針對 .NET Framework 4.0 編譯,但在具有 .NET Framework 4.5 或更新版本的電腦上執行,則會在 .NET Framework 4.0 相容性模式下執行。For example, if a binary was compiled for .NET Framework 4.0 but runs on a machine with .NET Framework 4.5 or later, it runs in .NET Framework 4.0 compatibility mode. 這表示,較新版本中的許多變更不會影響二進位檔。This means that many of the changes in the later version don't affect the binary.

應用程式目標的 .NET Framework 版本是由程式碼執行所在之應用程式域的專案元件目標版本所決定。The version of the .NET Framework that an application targets is determined by the target version of the entry assembly for the application domain that the code runs in. 載入該應用程式域中的所有其他元件都會以該版本為目標。All additional assemblies loaded in that application domain target that version. 例如,如果是可執行檔,則可執行檔目標的版本是該應用程式域中的所有元件在其下執行的相容性模式。For example, in the case of an executable, the version that the executable targets is the compatibility mode all assemblies in that application domain run under.

若要查看適用于您環境的執行時間變更清單,請選取您目前設為目標的 .NET Framework 版本,然後選擇您想要遷移到的版本:To see a list of runtime changes that apply to your environment, select the .NET Framework version you're currently targeting and then the version you wish to migrate to:

重定目標變更Retargeting changes

重定變更是元件重新編譯成以較新版本為目標時所發生的變更。Retargeting changes are those that arise when an assembly is recompiled to target a newer version. 以較新的版本為目標,表示元件會加入宣告新功能,以及舊功能的潛在相容性問題。Targeting a newer version means the assembly opts into the new features as well as potential compatibility issues for old features.

若要查看適用于您環境的重定變更清單,請選取您目前設為目標的 .NET Framework 版本,然後選擇您想要遷移到的版本:To see a list of retargeting changes that apply to your environment, select the .NET Framework version you're currently targeting and then the version you wish to migrate to:

影響分類Impact classification

在描述執行時間和重定變更的主題中(例如,重定從4.7.2 遷移到4.8 的變更),個別專案會依其預期的影響分類,如下所示:In the topics that describe runtime and retargeting changes, for example, Retargeting changes for migrating from 4.7.2 to 4.8, individual items are classified by their expected impact as follows:

MajorMajor
影響大量應用程式或需要大幅修改程式碼的重大變更。A significant change that affects a large number of apps or that requires substantial modification of code.

MinorMinor
影響少量應用程式或需要稍微修改程式碼的變更。A change that affects a small number of apps or that requires minor modification of code.

Edge 案例Edge case
在非常特定 (罕見) 的情況下影響應用程式的變更。A change that affects apps under very specific scenarios that are not common.

透明Transparent
此變更對應用程式的開發人員或使用者的影響不明顯。A change that has no noticeable effect on the app's developer or user. 發生此變更的應用程式應該不需要修改。The app should not require modification because of this change.

請參閱See also