規定執行時在執行字串比較時應使用舊有排序順序。
<configuration>
<runtime>
<CompatSortNLSVersion>
語法
<CompatSortNLSVersion
enabled="4096"/>
屬性和項目
下列章節說明屬性、子元素和父元素。
Attributes
| Attribute | Description |
|---|---|
enabled |
必要屬性。 指定要使用排序順序的區域 ID。 |
啟用屬性
| 價值觀 | Description |
|---|---|
| 4096 | 代表另一種排序順序的區域 ID。 在此情況下,4096 代表 .NET Framework 3.5 及更早版本的排序順序。 |
子元素
沒有。
父項目
| 元素 | Description |
|---|---|
configuration |
通用語言執行平台和 .NET Framework 應用程式所使用之每個組態檔中的根項目。 |
runtime |
包含有關執行階段初始化選項的資訊。 |
備註
由於 .NET Framework 4 中類別 System.Globalization.CompareInfo 執行的字串比較、排序與大小寫操作符合 Unicode 5.1 標準,字串比較方法 String.Compare(String, String) 如 和 String.LastIndexOf(String) 的結果可能與先前版本的 .NET Framework 不同。 如果你的應用程式依賴舊有行為,你可以透過在應用程式的設定檔中包含該 <CompatSortNLSVersion> 元素,還原 .NET Framework 3.5 及更早版本中使用的字串比較與排序規則。
這很重要
恢復舊有字串比較與排序規則也需要 sort00001000.dll 動態連結函式庫在本地系統中可用。
你也可以在特定應用域中使用舊有字串排序與比較規則,方法是在建立應用程式域時將字串「NetFx40_Legacy20SortingBehavior」傳給 SetCompatibilitySwitches 該方法。
Example
以下範例實例化兩個 String 物件,並呼叫 String.Compare(String, String, StringComparison) 方法透過當前文化的慣例來比較它們。
using System;
enum StringComparisonResult
{
precedes = -1,
equals = 0,
follows = 1,
};
public class Example
{
public static void Main()
{
string str1 = new string( new char[] {'\u0219', '\u021B', 'a' });
string str2 = "a";
Console.WriteLine("{0} {1} {2} in the sort order.",
str1,
(StringComparisonResult) String.Compare(str1, str2, StringComparison.CurrentCulture),
str2);
}
}
Enum StringComparisonResult As Integer
precedes = -1
equals = 0
follows = 1
End Enum
Module Example
Public Sub Main()
Dim str1 As String = ChrW(&h219) + ChrW(&h21B) + "a"
Dim str2 As String = "a"
Console.WriteLine("{0} {1} {2} in the sort order.", _
str1, _
CType(String.Compare(str1, str2, StringComparison.CurrentCulture), StringComparisonResult), _
str2)
End Sub
End Module
當你在 .NET Framework 4 上執行這個範例時,會顯示以下輸出:
sta follows a in the sort order.
這與你在 .NET Framework 3.5 執行範例時所顯示的輸出完全不同:
sta equals a in the sort order.
然而,如果你將以下設定檔加入範例目錄,然後在 .NET Framework 4 上執行,輸出結果與範例在 .NET Framework 3.5 上執行時相同。
<?xml version ="1.0"?>
<configuration>
<runtime>
<CompatSortNLSVersion enabled="4096"/>
</runtime>
</configuration>