이 문서는 이 API에 대한 참조 설명서를 보충하는 추가 설명을 제공합니다.
불변 문화는 문화적 차이에 민감하지 않습니다. 이는 영어와 관련이 있지만, 특정 국가나 지역과는 연관이 없습니다. CultureInfo 인스턴스화 메서드 호출에서 빈 문자열("")을 사용하여 이름으로 고정 문화권을 지정합니다. CultureInfo.InvariantCulture이 속성은 고정 문화권의 인스턴스도 검색합니다. 문화권이 필요한 System.Globalization 네임스페이스의 거의 모든 메서드에서 사용할 수 있습니다. CompareInfo, DateTimeFormat및 NumberFormat 같은 속성에서 반환되는 개체는 고정 문화권의 문자열 비교 및 서식 규칙도 반영합니다.
사용자 사용자 지정 또는 .NET Framework 또는 운영 체제 업데이트에 의해 변경될 수 있는 문화권에 민감한 데이터와 달리 고정 문화권 데이터는 시간이 지남에 따라 설치된 문화권 간에 안정적이며 사용자가 사용자 지정할 수 없습니다. 불변 문화권은 서식이 지정된 데이터의 유지에 필요한 서식 지정 및 구문 분석 작업이나 문화권에 상관없이 데이터를 고정된 순서로 정렬하고 배열해야 하는 작업처럼 문화권 독립적인 결과가 필요한 작업에 특히 유용합니다.
문자열 작업
문화에 민감한 문자열 작업을 수행할 때 특정 문화권의 규칙에 영향을 받지 않고, 문화권 간에 일관성을 유지하는 고정 문화권을 사용할 수 있습니다. 예를 들어 정렬된 데이터를 고정된 순서로 표시하거나 현재 문화권에 관계없이 문자열에 대/소문자 규칙의 표준 집합을 적용할 수 있습니다. 이렇게 하려면 InvariantCulture 개체를 Compare(String, String, Boolean, CultureInfo) 및 ToUpper(CultureInfo)같은 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: {input}\n");
// 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 {frFr.Name} culture: {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 {deDe.Name} culture: {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를 사용하여 대소문자 구분 여부에 따른 서수 비교를 수행해야 합니다. 문화권 구분 문자열 작업을 수행하는 코드는 현재 문화권이 변경되거나 코드를 실행하는 컴퓨터의 문화권이 코드를 테스트하는 데 사용되는 문화권과 다른 경우 보안 취약성을 일으킬 수 있습니다. 반면 서수 비교는 비교된 문자의 이진 값에만 의존합니다.
.NET