System.Globalization.CultureInfo.InvariantCulture 속성

이 문서에서는 이 API에 대한 참조 설명서에 대한 추가 설명서를 제공합니다.

고정 문화권은 문화권을 구분하지 않습니다. 영어와 연결되지만 국가/지역과는 연결되지 않습니다. 인스턴스화 메서드 호출 CultureInfo 에서 빈 문자열("")을 사용하여 이름으로 고정 문화권을 지정합니다. 이 속성은 CultureInfo.InvariantCulture고정 문화권의 인스턴스도 검색합니다. 문화권이 필요한 네임스페이스의 System.Globalization 거의 모든 메서드에서 사용할 수 있습니다. 와 같은 CompareInfoDateTimeFormat속성에서 반환되는 개체이며 NumberFormat 고정 문화권의 문자열 비교 및 서식 규칙도 반영합니다.

사용자 사용자 지정 또는 .NET Framework 또는 운영 체제 업데이트에 의해 변경될 수 있는 문화권에 민감한 데이터와 달리 고정 문화권 데이터는 시간이 지남에 따라 설치된 문화권 간에 안정적이며 사용자가 사용자 지정할 수 없습니다. 따라서 고정 문화권은 서식이 지정된 데이터를 유지하는 서식 지정 및 구문 분석 작업이나 문화권에 관계없이 데이터를 고정된 순서로 표시해야 하는 정렬 및 순서 지정 작업과 같이 문화권 독립적 결과가 필요한 작업에 특히 유용합니다.

문자열 작업

현재 문화권의 규칙에 영향을 받지 않고 문화권 간에 일관된 문화권 구분 문자열 작업에 고정 문화권을 사용할 수 있습니다. 예를 들어 정렬된 데이터를 고정된 순서로 표시하거나 현재 문화권에 관계없이 문자열에 대/소문자 규칙의 표준 집합을 적용할 수 있습니다. 이렇게 하려면 다음과 같은 Compare(String, String, Boolean, CultureInfo) 매개 변수가 있는 메서드에 개체를 CultureInfo 전달 InvariantCulture 합니다ToUpper(CultureInfo).

데이터 유지

이 속성은 InvariantCulture 문화권 독립적 형식으로 데이터를 유지하는 데 사용할 수 있습니다. 이는 변경되지 않고 문화권 간에 데이터를 직렬화하고 역직렬화하는 데 사용할 수 있는 알려진 형식을 제공합니다. 데이터가 역직렬화되면 현재 사용자의 문화권 규칙에 따라 적절하게 서식을 지정할 수 있습니다.

예를 들어 날짜 및 시간 데이터를 문자열 형식으로 유지하도록 선택한 경우 개체를 또는 DateTimeOffset.ToString(IFormatProvider) 메서드에 DateTime.ToString(String, IFormatProvider) 전달 InvariantCulture 하여 문자열을 만들 수 있으며, 개체 DateTime.Parse(String, IFormatProvider) 를 또는 DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) 메서드에 전달 InvariantCulture 하여 문자열을 날짜 및 시간 값으로 다시 변환할 수 있습니다. 이 기술을 사용하면 다른 문화권의 사용자가 데이터를 읽거나 쓸 때 기본 날짜 및 시간 값이 변경되지 않습니다.

다음 예제에서는 고정 문화권을 사용하여 값을 문자열로 유지 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.OrdinalIgnoreCase 대/소문자를 구분하지 않는 서수 비교를 StringComparison.Ordinal 수행해야 합니다. 문화권 구분 문자열 작업을 수행하는 코드는 현재 문화권이 변경되거나 코드를 실행하는 컴퓨터의 문화권이 코드를 테스트하는 데 사용되는 문화권과 다른 경우 보안 취약성을 일으킬 수 있습니다. 반면 서수 비교는 비교된 문자의 이진 값에만 의존합니다.