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 需要文化特性的任何方法。 、、 DateTimeFormatNumberFormat 等 CompareInfo 屬性所傳回的物件也會反映不因文化特性而異的字串比較和格式化慣例。
不同于區分文化特性的資料,可能會因使用者自訂或.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 做為引數,以執行區分大小寫或不區分大小寫的序數比較。 執行區分文化特性字串作業的程式碼,如果目前的文化特性已變更,或執行程式碼之電腦上的文化特性與用來測試程式碼的文化特性不同,可能會導致安全性弱點。 相反地,序數比較只取決於比較字元的二進位值。