.NET Framework 類別庫中已淘汰的功能What's obsolete in the .NET Framework class library

.NET 會隨著時間而改變。.NET changes over time. 每個新版本都會加入一些提供新功能的新類型和類型成員。Each new version adds new types and type members that provide new functionality. 現有的類型及其成員也會隨著時間改變。Existing types and their members also change over time. 例如,某些型別在支援的技術被取代為新的技術時變得較不重要,而某些方法則是以某種方式較佳的較新方法取代。For example, some types become less important as the technology they support is replaced by a new technology, and some methods are superseded by newer methods that are superior in some way.

.NET Framework 和 common language runtime 致力於支援回溯相容性 (讓以某個 .NET Framework 版本開發的應用程式在下一版的 .NET Framework) 上執行。.NET Framework and the common language runtime strive to support backward compatibility (allowing applications that were developed with one version of .NET Framework to run on the next version of .NET Framework). 這點會讓您難以單純地移除類型或類型成員。This makes it difficult to simply remove a type or a type member. 相反地,.NET 會將類型或類型成員標示為已淘汰或已淘汰,因此不應再使用。Instead, .NET indicates that a type or a type member should no longer be used by marking it as obsolete or deprecated. 取代類型或成員時需要為其加上標記,以便讓開發人員了解該類型或成員即將消失,而且有時間來回應其移除。Deprecating a type or a member involves marking it so that developers are aware it will go away and have time to respond to its removal. 不過,使用該類型或成員的現有程式碼會繼續在新的 .NET 版本中執行。However, existing code that uses the type or member continues to run in the new version of .NET.

注意

淘汰 和已 淘汰的 條款在套用至 .net 類型和成員時,具有相同的意義。The terms obsolete and deprecated have the same meaning when applied to .NET types and members.

ObsoleteAttribute 屬性The ObsoleteAttribute attribute

.NET Framework 會使用 ObsoleteAttribute 屬性來標記某個類型或類型成員,表示該類型或類型成員已過時。The .NET Framework indicates that a type or type member is obsolete by marking it with the ObsoleteAttribute attribute. 如果將這個屬性套用至某個類型或成員,就表示未來的某個 .NET Framework 版本將移除該類型或成員,但不會破壞使用該成員的已編譯程式碼。Applying the attribute to a type or member indicates that type or member will be removed in some future version of the .NET Framework without breaking compiled code that uses that member.

除了表示某個類型或類型成員已過時之外,ObsoleteAttribute 也會定義編譯器如何處理包含該類型或成員的原始程式碼。In addition to indicating that a type or a type member is obsolete, ObsoleteAttribute defines how the compiler handles source code that includes that type or member. 編譯器可能會編譯程式碼但發出警告訊息,也可能會將類型或成員的使用視為錯誤。The compiler can compile the code but emit a warning message, or it can treat the use of the type or member as an error. 在第一種情況下,雖然程式碼可以成功編譯,但是警告訊息會指出類型或成員已過時。In the first case, the code can successfully compile, but a warning message indicates that the type or member is obsolete. 在第二種情況下,編譯會失敗。In the second case, compilation fails.

即使編譯產生錯誤而非警告訊息,ObsoleteAttribute 並不會影響執行階段行為。Even if compilation produces an error instead of a warning message, ObsoleteAttribute does not affect run-time behavior. 也就是說,使用該類型或成員而且已成功編譯的應用程式一定會順利執行。That is, applications that use the type or member and that have compiled successfully will always run successfully. 只有嘗試重新編譯使用該類型或成員之應用程式的作業會失敗。Only the attempt to recompile an application that uses the type or member fails.

如何處理已淘汰的類型和成員How to handle obsolete types and members

當您升級和重新編譯現有的程式碼時,使用在應用程式中產生編譯器警告的過時類型或成員是完全可接受的作法。When you upgrade and recompile existing code, using an obsolete type or member that produces a compiler warning in your application is perfectly acceptable. 不過,您應該檢閱編譯器警告訊息,以便判斷是否應該變更應用程式程式碼。However, you should review the compiler warning message to determine whether you should change your application code. 如果訊息沒有指向適當的替代方案,您就應該進行下列其中一項步驟:If the message does not point to a suitable alternative, you should do either of the following:

  • 變更程式碼,不使用類型或成員 (如果可能的話)。Change your code by removing the use of the type or member, if possible.

    -或--or-

  • 檢閱適用於這個技術領域的文件,以便判斷如何回應取代。Review the documentation for this technology area to determine how to respond to the deprecation.

您可以選擇不要針對更新的 .NET Framework 版本重新編譯現有的程式碼,You may choose not to recompile existing code against a later version of the .NET Framework. 而改為指定現有已編譯程式碼所執行的目標 .NET Framework 版本。Instead, you can specify the version of the .NET Framework against which your existing compiled code runs. 例如,假設您有一個名為 app1.exe 且已針對 .NET Framework 3.5 編譯的應用程式,但是您想要讓這個應用程式針對 .NET Framework 4.5 執行。For example, suppose that you have an application named app1.exe that was compiled against the .NET Framework 3.5, but you want the application to run against the .NET Framework 4.5. 此時,您需要進行下列步驟:This requires the following steps:

  1. 建立主要可執行檔的組態檔,並將它命名為 appName.exe.config,其中 appName 是應用程式可執行檔的名稱。Create a configuration file for your main executable and name it appName.exe.config, where appName is the name of the application executable. 針對範例中名為 app1.exe 的應用程式,您會建立名為 app1.exe.config的設定檔。For the application named app1.exe in our example, you would create a configuration file named app1.exe.config.

  2. 將下列內容加入組態檔。Add the following to the configuration file.

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

若要以特定版本的 .NET Framework 為目標,請將下列其中一個字串值指派給 version 屬性:To target a specific version of .NET Framework, assign one of the following string values to the version attribute:

.NET Framework 版本.NET Framework version version 字串version string
4.84.8 4.0 版起v4.0
4.7 (包括 4.7.1 和 4.7.2)4.7 (including 4.7.1 and 4.7.2) 4.0 版起v4.0
4.6 (包括 4.6.1 和 4.6.2)4.6 (including 4.6.1 and 4.6.2) 4.0 版起v4.0
4.5 (包括 4.5.1 和 4.5.2)4.5 (including 4.5.1 and 4.5.2) 4.0 版起v4.0
44 4.0 版起v4.0
3.53.5 v2.0.50727v2.0.50727
2.02.0 v2.0.50727v2.0.50727
1.11.1 v1.1.4322v1.1.4322
1.01.0 v1.0.3705v1.0.3705

適用于 .NET Framework 4.5 和更新版本的過時 ApiObsolete APIs for .NET Framework 4.5 and later versions

舊版的過時 ApiObsolete APIs for previous versions

另請參閱See also