<CompatSortNLSVersion> 元素<CompatSortNLSVersion> Element

指定在执行字符串比较时,运行时应使用旧排序顺序。Specifies that the runtime should use legacy sort orders when performing string comparisons.




特性和元素Attributes and Elements

下列各节描述了特性、子元素和父元素。The following sections describe attributes, child elements, and parent elements.


属性Attribute 说明Description
enabled 必需的特性。Required attribute.

指定要使用其排序顺序的区域设置 ID。Specifies the locale ID whose sort order is to be used.

enabled 特性enabled Attribute

Value 说明Description
40964096 表示备选排序顺序的区域设置 ID。The locale ID that represents an alternate sort order. 在这种情况下,4096表示 .NET Framework 3.5 及更早版本的排序顺序。In this case, 4096 represents the sort order of the .NET Framework 3.5 and earlier versions.

子元素Child Elements


父元素Parent Elements

元素Element 描述Description
configuration 公共语言运行时和 .NET Framework 应用程序所使用的每个配置文件中的根元素。The root element in every configuration file used by the common language runtime and .NET Framework applications.
runtime 包含有关运行时初始化选项的信息。Contains information about runtime initialization options.


由于 .NET Framework 4 中的类执行的字符串比较、排序和大小写操作 System.Globalization.CompareInfo 符合 Unicode 5.1 标准,因此和等字符串比较方法的结果 String.Compare(String, String) String.LastIndexOf(String) 可能与以前版本的 .NET Framework 不同。Because string comparison, sorting, and casing operations performed by the System.Globalization.CompareInfo class in the .NET Framework 4 conform to the Unicode 5.1 standard, the results of string comparison methods such as String.Compare(String, String) and String.LastIndexOf(String) may differ from previous versions of the .NET Framework. 如果你的应用程序依赖于旧行为,则可以通过在 <CompatSortNLSVersion> 应用程序配置文件中包括元素,来还原 .NET Framework 3.5 及更早版本中使用的字符串比较和排序规则。If your application depends on legacy behavior, you can restore the string comparison and sorting rules used in the .NET Framework 3.5 and earlier versions by including the <CompatSortNLSVersion> element in your application's configuration file.


还原旧的字符串比较和排序规则还要求 sort00001000.dll 动态链接库在本地系统上可用。Restoring legacy string comparison and sorting rules also requires the sort00001000.dll dynamic link library to be available on the local system.

此外,通过在创建应用程序域时将字符串“NetFx40_Legacy20SortingBehavior”传递到 SetCompatibilitySwitches 方法,可以在特定的应用程序域中使用旧的字符串排序和比较规则。You can also use legacy string sorting and comparison rules in a specific application domain by passing the string "NetFx40_Legacy20SortingBehavior" to the SetCompatibilitySwitches method when you create the application domain.


下面的示例实例化两个 String 对象,并调用 String.Compare(String, String, StringComparison) 方法以通过使用当前区域性的约定对它们进行比较。The following example instantiates two String objects and calls the String.Compare(String, String, StringComparison) method to compare them by using the conventions of the current culture.

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.",
                        (StringComparisonResult) String.Compare(str1, str2, StringComparison.CurrentCulture),
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), _
    End Sub
End Module

在 .NET Framework 4 上运行该示例时,它会显示以下输出:When you run the example on the .NET Framework 4, it displays the following output:

sta follows a in the sort order.  

这与在 .NET Framework 3.5 上运行示例时显示的输出完全不同:This is completely different from the output that is displayed when you run the example on the .NET Framework 3.5:

sta equals a in the sort order.  

但是,如果将下面的配置文件添加到示例的目录中,然后在 .NET Framework 4 上运行此示例,则输出将与示例在 .NET Framework 3.5 上运行时所生成的输出相同。However, if you add the following configuration file to the example's directory and then run the example on the .NET Framework 4, the output is identical to that produced by the example when it is run on the .NET Framework 3.5.

<?xml version ="1.0"?>  
      <CompatSortNLSVersion enabled="4096"/>  

另请参阅See also