CultureInfo.InvariantCulture Özellik
Tanım
CultureInfoKültüre bağımsız (Sabit) nesneyi alır.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
Özellik Değeri
Kültür bağımsız (Sabit) nesne.The object that is culture-independent (invariant).
Açıklamalar
Sabit kültür kültür duyarsızdır; Bu, herhangi bir ülkede/bölgede değil, Ingilizce diliyle ilişkilidir.The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region. Örnek oluşturma yöntemine yapılan çağrıda boş bir dize ("") kullanarak sabit kültürü ada göre belirtirsiniz CultureInfo .You specify the invariant culture by name by using an empty string ("") in the call to a CultureInfo instantiation method. CultureInfo.InvariantCulture Ayrıca, sabit kültürün bir örneğini alır.CultureInfo.InvariantCulture also retrieves an instance of the invariant culture. Kültür gerektiren ad alanındaki neredeyse her yöntemde kullanılabilir System.Globalization .It can be used in almost any method in the System.Globalization namespace that requires a culture. , Ve gibi özellikler tarafından döndürülen nesneler CompareInfo , DateTimeFormat NumberFormat sabit kültürün dize karşılaştırma ve biçimlendirme kurallarını da yansıtır.The objects returned by properties such as CompareInfo, DateTimeFormat, and NumberFormat also reflect the string comparison and formatting conventions of the invariant culture.
Kullanıcı özelleştirmesine veya .NET Framework ya da işletim sistemine yönelik güncelleştirmeler tarafından değişikliğe tabi olan kültüre duyarlı verilerden farklı olarak, sabit kültür verileri zaman içinde ve yüklü kültürler arasında kararlı olur ve kullanıcılar tarafından özelleştirilemez.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. Bu, sabit kültürün biçimlendirilmiş verileri kalıcı hale getiren, biçimlendirme ve ayrıştırma işlemleri ya da bu verilerin kültürden bağımsız olarak sabit bir sırada görüntülenmesini gerektiren sıralama ve sıralama işlemleri gibi kültüre bağımsız sonuçlar gerektiren işlemler için özellikle kullanışlıdır.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.
Dize İşlemleriString Operations
Geçerli iş parçacığı kültürünün kurallarından etkilenmemiş ve kültürler arasında tutarlı olan kültüre duyarlı dize işlemleri için sabit kültür kullanabilirsiniz.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. Örneğin, sıralanmış verilerin sabit bir düzende görünmesini veya geçerli iş parçacığı kültürüne bakılmaksızın dizelerde standart bir büyük-küçük harf kuralları uygulanmasını isteyebilirsiniz.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. Bunu yapmak için InvariantCulture nesnesini, ve gibi parametresine sahip bir yönteme geçirirsiniz 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).
Kalıcı VeriPersisting Data
InvariantCultureÖzelliği, verileri kültür bağımsız biçiminde kalıcı hale getirmek için kullanılabilir.The InvariantCulture property can be used to persist data in a culture-independent format. Bu, değişmeyen ve kültürler genelinde verileri seri hale getirmek ve seri durumdan çıkarmak için kullanılabilen bilinen bir biçim sağlar.This provides a known format that does not change and that can be used to serialize and deserialize data across cultures. Verilerin serisi kaldırıldıktan sonra, geçerli kullanıcının kültürel kurallarına göre uygun şekilde biçimlendirilebilir.After the data is deserialized, it can be formatted appropriately based on the cultural conventions of the current user.
Örneğin, tarih ve saat verilerini dize biçiminde kalıcı hale getirmek isterseniz, InvariantCulture DateTime.ToString(String, IFormatProvider) DateTimeOffset.ToString(IFormatProvider) dizeyi oluşturmak için nesneyi or yöntemine geçirebilir ve InvariantCulture DateTime.Parse(String, IFormatProvider) DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) dizeyi bir tarih ve saat değerine geri dönüştürmek için veya yöntemine geçirebilirsiniz.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. Bu teknik, veriler farklı kültürlerin kullanıcıları tarafından okunarak veya yazıldığında temeldeki tarih ve saat değerlerinin değişmemesini sağlar.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.
Aşağıdaki örnek, bir değeri dize olarak kalıcı hale getirmek için sabit kültür kullanır DateTime .The following example uses the invariant culture to persist a DateTime value as a string. Ardından, dizeyi ayrıştırır ve Fransızca (Fransa) ve Almanca (Almanya) kültürlerin biçimlendirme kurallarını kullanarak değerini görüntüler.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
Güvenlik KararlarıSecurity Decisions
Bir dize karşılaştırma veya örnek değişikliği sonucuna bağlı olarak bir güvenlik kararı (bir sistem kaynağına erişime izin verip vermeme gibi) yapıyorsanız, sabit kültür kullanmamalısınız.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. Bunun yerine, bir StringComparison parametre içeren ve bir StringComparison.Ordinal bağımsız değişken olarak veya sağlayan bir yöntemi çağırarak büyük küçük harfe duyarlı veya büyük/küçük harfe duyarsız bir sıra karşılaştırması gerçekleştirmeniz gerekir StringComparison.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. Geçerli kültür değiştirilirse veya kodu çalıştıran bilgisayardaki kültürün kodu test etmek için kullanılan kültürden farklıysa, kültüre duyarlı dize işlemleri gerçekleştiren kod güvenlik açıklarına neden olabilir.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. Buna karşılık, sıralı karşılaştırma yalnızca karşılaştırılan karakterlerin ikili değerine bağlıdır.In contrast, an ordinal comparison depends solely on the binary value of the compared characters.