CultureInfo.InvariantCulture 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取不依赖于区域性(固定)的 CultureInfo 对象。
public:
static property System::Globalization::CultureInfo ^ InvariantCulture { System::Globalization::CultureInfo ^ get(); };
public static System.Globalization.CultureInfo InvariantCulture { get; }
static member InvariantCulture : System.Globalization.CultureInfo
Public Shared ReadOnly Property InvariantCulture As CultureInfo
属性值
不依赖于区域性(固定)的对象。
注解
固定区域性不区分区域性;它与英语相关联,但不与任何国家/地区相关联。 通过在调用 CultureInfo 实例化方法时使用空字符串 (“”) ,按名称指定固定区域性。 CultureInfo.InvariantCulture 还检索固定区域性的实例。 它几乎可以在命名空间中 System.Globalization 需要区域性的任何方法中使用。 、 和 NumberFormat 等CompareInfoDateTimeFormat属性返回的对象还反映了固定区域性的字符串比较和格式设置约定。
与区域性敏感数据(可能会因用户自定义或.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: {0}\n" , input);
// 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 {0} culture: {1}" ,
frFr.Name, 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 {0} culture: {1}" ,
deDe.Name, 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 作为参数,来执行区分大小写或不区分大小写的序号比较。 如果更改当前区域性或运行代码的计算机上的区域性不同于用于测试代码的区域性,则执行区分区域性的字符串操作的代码可能会导致安全漏洞。 相反,序号比较仅取决于比较字符的二进制值。
适用于
另请参阅
反馈
提交和查看相关反馈