指定运行时在执行字符串比较时应使用旧排序顺序。
<配置>
<运行时>
<CompatsortNLSVersion>
Syntax
<CompatSortNLSVersion
enabled="4096"/>
特性和元素
下列各节描述了特性、子元素和父元素。
特性
| Attribute | Description |
|---|---|
enabled |
必需属性。 指定要使用的排序顺序的区域设置 ID。 |
enabled Attribute
| 价值 | 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 不同)。 如果应用程序依赖于旧行为,可以通过在应用程序的配置文件中包含元素来还原 .NET Framework 3.5 和早期版本中 <CompatSortNLSVersion> 使用的字符串比较和排序规则。
重要
还原旧字符串比较和排序规则还需要在本地系统上提供 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>