CultureInfo.InvariantCulture 属性

定义

获取不依赖于区域性(固定)的 CultureInfo 对象。Gets the CultureInfo object that is culture-independent (invariant).

public:
 static property System::Globalization::CultureInfo ^ InvariantCulture { System::Globalization::CultureInfo ^ get(); };
public static System.Globalization.CultureInfo InvariantCulture { get; }
member this.InvariantCulture : System.Globalization.CultureInfo
Public Shared ReadOnly Property InvariantCulture As CultureInfo

属性值

不依赖于区域性(固定)的对象。The object that is culture-independent (invariant).

注解

固定区域性不区分区域性;它与英语语言相关联,而不是与任何国家/地区相关联。The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region. 在对 CultureInfo 实例化方法的调用中使用空字符串(""),按名称指定固定区域性。You specify the invariant culture by name by using an empty string ("") in the call to a CultureInfo instantiation method. CultureInfo.InvariantCulture 还将检索固定区域性的实例。CultureInfo.InvariantCulture also retrieves an instance of the invariant culture. 它几乎可用于需要区域性的 System.Globalization 命名空间中的几乎所有方法。It can be used in almost any method in the System.Globalization namespace that requires a culture. 属性返回的对象(例如 CompareInfoDateTimeFormatNumberFormat)还反映了固定区域性的字符串比较和格式设置约定。The objects returned by properties such as CompareInfo, DateTimeFormat, and NumberFormat also reflect the string comparison and formatting conventions of the invariant culture.

与区域性相关的数据(受用户自定义的更改或对 .NET Framework 或操作系统的更新不同)不同的是,固定区域性数据在一段时间内和安装的区域性之间稳定稳定,用户不能进行自定义。Unlike culture-sensitive data, which is subject to change by user customization or by updates to the .NET Framework or the operating system, invariant culture data is stable over time and across installed cultures and cannot be customized by users. 这使得固定区域性对于需要与区域性无关的结果的操作特别有用,例如,格式设置和分析保存格式化数据的操作,或者需要将数据显示在固定顺序,与区域性无关。This makes the invariant culture particularly useful for operations that require culture-independent results, such as formatting and parsing operations that persist formatted data, or sorting and ordering operations that require that data be displayed in a fixed order regardless of culture.

字符串运算String Operations

您可以使用固定区域性来执行不受当前线程区域性的约定和跨区域性一致的区分区域性的字符串操作。You can use the invariant culture for culture-sensitive string operations that are not affected by the conventions of the current thread culture and that are consistent across cultures. 例如,您可能希望排序的数据按固定顺序显示,或者将一组标准的大小写约定应用于字符串,而不考虑当前线程的区域性。For example, you may want sorted data to appear in a fixed order or apply a standard set of casing conventions to strings regardless of the current thread culture. 为此,请将 InvariantCulture 对象传递到具有 CultureInfo 参数的方法,如 Compare(String, String, Boolean, CultureInfo)ToUpper(CultureInfo)To do this, you pass the InvariantCulture object to a method that has a CultureInfo parameter, such as Compare(String, String, Boolean, CultureInfo) and ToUpper(CultureInfo).

保留数据Persisting Data

InvariantCulture 属性可用于保存采用与区域性无关的格式的数据。The InvariantCulture property can be used to persist data in a culture-independent format. 这提供了一种已知的格式,该格式不会改变,并且可用于跨区域性序列化和反序列化数据。This provides a known format that does not change and that can be used to serialize and deserialize data across cultures. 反序列化数据后,可以根据当前用户的区域性约定适当地设置数据的格式。After the data is deserialized, it can be formatted appropriately based on the cultural conventions of the current user.

例如,如果选择以字符串形式保留日期和时间数据,则可以将 InvariantCulture 对象传递到 DateTime.ToString(String, IFormatProvider)DateTimeOffset.ToString(IFormatProvider) 方法来创建字符串,并且可以将 InvariantCulture 对象传递到 DateTime.Parse(String, IFormatProvider)DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) 方法将字符串转换回日期和时间值。For example, if you choose to persist date and time data in string form, you can pass the InvariantCulture object to the DateTime.ToString(String, IFormatProvider) or DateTimeOffset.ToString(IFormatProvider) method to create the string, and you can pass the InvariantCulture object to the DateTime.Parse(String, IFormatProvider) or DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) method to convert the string back to a date and time value. 此方法可确保当用户读取或写入数据时,不会更改基础日期和时间值。This technique ensures that the underlying date and time values do not change when the data is read or written by users from different cultures.

下面的示例使用固定区域性将 DateTime 值保存为字符串。The following example uses the invariant culture to persist a DateTime value as a string. 然后,它使用法语(法国)和德语(德国)区域性的格式设置约定分析字符串并显示其值。It then parses the string and displays its value by using the formatting conventions of the French (France) and German (Germany) cultures.

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

安全决策Security Decisions

如果要基于字符串比较或大小写更改的结果做出安全决策(例如是否允许访问系统资源),则不应使用固定区域性。If you are making a security decision (such as whether to allow access to a system resource) based on the result of a string comparison or a case change, you should not use the invariant culture. 相反,应通过调用包含 StringComparison 参数的方法,并提供作为参数的 StringComparison.OrdinalStringComparison.OrdinalIgnoreCase,来执行区分大小写或不区分大小写的序号比较。Instead, you should perform a case-sensitive or case-insensitive ordinal comparison by calling a method that includes a StringComparison parameter and supplying either StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase as an argument. 如果当前区域性发生更改,或者运行代码的计算机上的区域性不同于用于测试代码的区域性,则执行区分区域性的字符串操作的代码可能会导致安全漏洞。Code that performs culture-sensitive string operations can cause security vulnerabilities if the current culture is changed or if the culture on the computer that is running the code differs from the culture that is used to test the code. 相反,序号比较仅依赖于所比较字符的二进制值。In contrast, an ordinal comparison depends solely on the binary value of the compared characters.

适用于

另请参阅