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.

適用於

另請參閱