本文提供了此 API 参考文档的补充说明。
不变文化对文化差异不敏感;它与英语相关,但不属于任何特定国家/地区。 通过在调用 CultureInfo 实例化方法时使用空字符串 ("") 按名称指定固定区域性。 此属性 CultureInfo.InvariantCulture还会检索不变文化的实例。 它几乎可以用于 System.Globalization 命名空间中任何需要区域性的方法。 由CompareInfo、DateTimeFormat和NumberFormat等属性返回的对象也反映了不变文化中的字符串比较和格式设置约定。
与文化敏感数据不同,文化不变数据不受用户自定义或 .NET Framework 及操作系统更新影响而改变,不随时间推移或已安装文化的不同而改变,保持稳定,并且用户无法自定义。 这使得固定区域性对于需要独立于区域性的结果的操作特别有用,例如格式化和分析保留格式化数据的操作,或排序和排序操作,这些操作要求无论区域性如何,都以固定顺序显示数据。
字符串操作
可以使用不变区域性来进行不受当前区域性约定影响且能跨区域保持一致的区域性敏感字符串操作。 例如,你可能希望排序后的数据以固定的顺序显示,或者对字符串应用一组标准的大小写约定,而与当前区域性无关。 为此,请将对象传递给 InvariantCulture 具有 CultureInfo 参数的方法,例如 Compare(String, String, Boolean, CultureInfo) 和 ToUpper(CultureInfo)。
持久化数据
该 InvariantCulture 属性可用于以独立于区域性的格式保存数据。 这提供了一种不更改的已知格式,可用于跨区域性序列化和反序列化数据。 反序列化数据后,可以根据当前用户的文化约定对数据进行适当的格式设置。
例如,如果选择以字符串形式保存日期和时间数据,则可以将该InvariantCulture对象传递给或DateTime.ToString(String, IFormatProvider)方法以创建字符串,并且可以将该DateTimeOffset.ToString(IFormatProvider)对象传递给InvariantCulture或DateTime.Parse(String, IFormatProvider)方法以将字符串转换DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles)回日期和时间值。 此技术可确保当来自不同文化的用户读取或写入数据时,底层日期和时间值不会更改。
以下示例使用固定区域性将 DateTime 值作为字符串持久化。 然后,它使用法国(法国)和德国(德国)文化的格式设置约定分析字符串并显示其值。
using System;
using System.IO;
using System.Globalization;
public class Example
{
public static void Main()
{
// Persist the date and time data.
StreamWriter sw = new StreamWriter(@".\DateData.dat");
// Create a DateTime value.
DateTime dtIn = DateTime.Now;
// Retrieve a CultureInfo object.
CultureInfo invC = CultureInfo.InvariantCulture;
// Convert the date to a string and write it to a file.
sw.WriteLine(dtIn.ToString("r", invC));
sw.Close();
// Restore the date and time data.
StreamReader sr = new StreamReader(@".\DateData.dat");
String input;
while ((input = sr.ReadLine()) != null)
{
Console.WriteLine($"Stored data: {input}\n");
// Parse the stored string.
DateTime dtOut = DateTime.Parse(input, invC, DateTimeStyles.RoundtripKind);
// Create a French (France) CultureInfo object.
CultureInfo frFr = new CultureInfo("fr-FR");
// Displays the date formatted for the "fr-FR" culture.
Console.WriteLine($"Date formatted for the {frFr.Name} culture: {dtOut.ToString("f", frFr)}");
// Creates a German (Germany) CultureInfo object.
CultureInfo deDe= new CultureInfo("de-De");
// Displays the date formatted for the "de-DE" culture.
Console.WriteLine($"Date formatted for {deDe.Name} culture: {dtOut.ToString("f", deDe)}");
}
sr.Close();
}
}
// The example displays the following output:
// Stored data: Tue, 15 May 2012 16:34:16 GMT
//
// Date formatted for the fr-FR culture: mardi 15 mai 2012 16:34
// Date formatted for de-DE culture: Dienstag, 15. Mai 2012 16:34
Imports System.Globalization
Imports System.IO
Module Example
Public Sub Main()
' Persist the date and time data.
Dim sw As New StreamWriter(".\DateData.dat")
' Create a DateTime value.
Dim dtIn As DateTime = DateTime.Now
' Retrieve a CultureInfo object.
Dim invC As CultureInfo = CultureInfo.InvariantCulture
' Convert the date to a string and write it to a file.
sw.WriteLine(dtIn.ToString("r", invC))
sw.Close()
' Restore the date and time data.
Dim sr As New StreamReader(".\DateData.dat")
Dim input As String = String.Empty
Do While sr.Peek() >= 0
input = sr.ReadLine()
Console.WriteLine("Stored data: {0}" , input)
Console.WriteLine()
' Parse the stored string.
Dim dtOut As DateTime = DateTime.Parse(input, invC, DateTimeStyles.RoundtripKind)
' Create a French (France) CultureInfo object.
Dim frFr As New CultureInfo("fr-FR")
' Displays the date formatted for the "fr-FR" culture.
Console.WriteLine("Date formatted for the {0} culture: {1}" ,
frFr.Name, dtOut.ToString("f", frFr))
' Creates a German (Germany) CultureInfo object.
Dim deDe As New CultureInfo("de-De")
' Displays the date formatted for the "de-DE" culture.
Console.WriteLine("Date formatted for {0} culture: {1}" ,
deDe.Name, dtOut.ToString("f", deDe))
Loop
sr.Close()
End Sub
End Module
' The example displays the following output:
' Stored data: Tue, 15 May 2012 16:34:16 GMT
'
' Date formatted for the fr-FR culture: mardi 15 mai 2012 16:34
' Date formatted for de-DE culture: Dienstag, 15. Mai 2012 16:34
安全决策
如果根据字符串比较或大小写更改的结果做出安全决策(例如是否允许访问系统资源),则不应使用固定区域性。 相反,应该通过调用包含 StringComparison 参数的方法,并提供 StringComparison.Ordinal 或 StringComparison.OrdinalIgnoreCase 作为参数,来执行区分大小写或不区分大小写的顺序比较。 执行对区域性敏感的字符串操作的代码,如果当前区域性被更改,或者运行代码的计算机上的区域性与测试代码时使用的区域性不符,可能会导致安全漏洞。 相比之下,序数比较仅依赖于所比较字符的二进制值。