CultureInfo.InvariantCulture 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
문화권 독립(고정)적인 CultureInfo 개체를 가져옵니다.
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
속성 값
문화권 독립(고정)적인 개체입니다.
설명
고정 문화권은 문화권에 구분하지 않습니다. 이 언어는 영어와 연결되지만 국가/지역에는 연결되지 않습니다. 인스턴스화 메서드 호출에서 빈 문자열("")을 사용하여 이름으로 고정 문화권 을 CultureInfo 지정합니다. CultureInfo.InvariantCulture 는 고정 문화권의 인스턴스도 검색합니다. 문화권이 필요한 네임스페이스의 거의 모든 메서드에서 사용할 수 System.Globalization 있습니다. , 및 와 같은 속성에서 반환되는 개체는 CompareInfo DateTimeFormat 고정 NumberFormat 문화권의 문자열 비교 및 서식 규칙도 반영합니다.
사용자 사용자 지정 또는 .NET Framework 또는 운영 체제 업데이트에 의해 변경될 수 있는 문화권 구분 데이터와 달리 고정 문화권 데이터는 시간이 지남에 따라 그리고 설치된 문화권에서 안정적이며 사용자가 사용자 지정할 수 없습니다. 따라서 고정 문화권은 서식이 지정된 데이터를 유지하는 서식 지정 및 구문 분석 작업이나 문화권에 관계없이 데이터를 고정된 순서로 표시해야 하는 정렬 및 순서 지정 작업과 같이 문화권 독립적 결과가 필요한 작업에 특히 유용합니다.
문자열 작업
현재 문화권의 규칙에 영향을 받지 않고 문화권 간에 일관된 문화권 구분 문자열 작업에 고정 문화권 을 사용할 수 있습니다. 예를 들어 정렬된 데이터를 고정 순서로 표시하거나 현재 문화권에 관계없이 표준 대/소문자 규칙을 문자열에 적용할 수 있습니다. 이렇게 하려면 InvariantCulture 개체를 및 와 같은 CultureInfo 매개 변수가 있는 메서드에 Compare(String, String, Boolean, CultureInfo) 전달합니다. ToUpper(CultureInfo)
데이터 유지
InvariantCulture속성은 문화권 독립적 형식으로 데이터를 유지하는 데 사용할 수 있습니다. 이는 변경되지 않고 문화권 간에 데이터를 직렬화 및 deserialize하는 데 사용할 수 있는 알려진 형식을 제공합니다. 데이터가 deserialized되면 현재 사용자의 문화권 규칙에 따라 적절하게 서식을 지정할 수 있습니다.
예를 들어 문자열 형식으로 날짜 및 시간 데이터를 유지하도록 선택한 경우 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 합니다. 문화권 구분 문자열 작업을 수행하는 코드는 현재 문화권이 변경되거나 코드를 실행하는 컴퓨터의 문화권이 코드를 테스트하는 데 사용되는 문화권과 다른 경우 보안 취약성을 일으킬 수 있습니다. 반면 서수 비교는 비교된 문자의 이진 값에만 의존합니다.