방법: 포함 리소스에서 표준 시간대 복원

이 토픽에서는 리소스 파일에 저장된 표준 시간대를 복원하는 방법을 설명합니다. 표준 시간대 저장에 대한 자세한 내용 및 지침은 방법: 포함된 리소스에 표준 시간대 저장을 참조하세요.

포함된 리소스에서 TimeZoneInfo 개체를 역직렬화하려면 다음과 같이 수행합니다.

  1. 검색할 표준 시간대가 사용자 지정 표준 시간대가 아닌 경우 FindSystemTimeZoneById 메서드를 사용하여 인스턴스화해 봅니다.

  2. 포함된 리소스 파일의 정규화된 이름과 리소스 파일이 포함된 어셈블리에 대한 참조를 전달하여 ResourceManager 개체를 인스턴스화합니다.

    포함된 리소스 파일의 정규화된 이름을 확인할 수 없는 경우 Ildasm.exe(IL 디스어셈블러)를 사용하여 어셈블리의 매니페스트를 검사합니다. .mresource 항목은 리소스를 식별합니다. 예제에서 리소스의 정규화된 이름은 SerializeTimeZoneData.SerializedTimeZones입니다.

    리소스 파일이 표준 시간대 인스턴스화 코드를 포함하는 동일한 어셈블리에 포함된 경우 static(Visual Basic에서 Shared)GetExecutingAssembly 메서드를 호출하여 해당 파일에 대한 참조를 검색할 수 있습니다.

  3. FindSystemTimeZoneById메서드 호출이 실패하거나 사용자 지정 표준 시간대를 인스턴스화할 경우 ResourceManager.GetString 메서드를 호출하여 직렬화된 표준 시간대가 포함된 문자열을 검색합니다.

  4. FromSerializedString 메서드를 호출하여 표준 시간대 데이터를 역직렬화합니다.

예시

다음 예제에서는 포함된 .NET XML 리소스 파일에 저장된 TimeZoneInfo 개체를 역직렬화합니다.

private void DeserializeTimeZones()
{
   TimeZoneInfo cst, palmer;
   string timeZoneString;
   ResourceManager resMgr = new ResourceManager("SerializeTimeZoneData.SerializedTimeZones", this.GetType().Assembly);

   // Attempt to retrieve time zone from system
   try
   {
      cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
   }
   catch (TimeZoneNotFoundException)
   {
      // Time zone not in system; retrieve from resource
      timeZoneString = resMgr.GetString("CentralStandardTime");
      if (! String.IsNullOrEmpty(timeZoneString))
      {
         cst = TimeZoneInfo.FromSerializedString(timeZoneString);
      }
      else
      {
         MessageBox.Show("Unable to create Central Standard Time Zone. Application must exit.", "Application Error");
         return;
      }
   }
   // Retrieve custom time zone
   try
   {
      timeZoneString = resMgr.GetString("PalmerStandardTime");
      palmer = TimeZoneInfo.FromSerializedString(timeZoneString);
   }
   catch (MissingManifestResourceException)
   {
      MessageBox.Show("Unable to retrieve the Palmer Standard Time Zone from the resource file. Application must exit.");
      return;
   }
}
Private Sub DeserializeTimeZones()
    Dim cst, palmer As TimeZoneInfo
    Dim timeZoneString As String
    Dim resMgr As ResourceManager = New ResourceManager("SerializeTimeZoneData.SerializedTimeZones",
                                    GetType(SerializeTimeZoneData).Assembly)

    ' Attempt to retrieve time zone from system
    Try
        cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")
    Catch ex As TimeZoneNotFoundException
        ' Time zone not in system; retrieve from resource
        timeZoneString = resMgr.GetString("CentralStandardTime")
        If Not String.IsNullOrEmpty(timeZoneString) Then
            cst = TimeZoneInfo.FromSerializedString(timeZoneString)
        Else
            MsgBox("Unable to create Central Standard Time Zone. Application must exit.")
            Exit Sub
        End If
    End Try
    ' Retrieve custom time zone
    Try
        timeZoneString = resMgr.GetString("PalmerStandardTime")
        palmer = TimeZoneInfo.FromSerializedString(timeZoneString)
    Catch ex As Exception
        MsgBox(ex.GetType().Name & ": Unable to create Palmer Standard Time Zone. Application must exit.")
        Exit Sub
    End Try
End Sub

이 코드는 애플리케이션에 필요한 TimeZoneInfo 개체가 있는지 확인하기 위한 예외 처리를 보여 줍니다. 먼저 FindSystemTimeZoneById 메서드를 사용하여 레지스트리에서 개체를 검색하여 TimeZoneInfo 개체를 인스턴스화하려고 합니다. 표준 시간대를 인스턴스화할 수 없는 경우 코드는 포함된 리소스 파일에서 검색합니다.

사용자 지정 표준 시간대(CreateCustomTimeZone 메서드를 사용하여 인스턴스화된 표준 시간대)에 대한 데이터는 레지스트리에 저장되지 않으므로 코드는 남극 팔머의 표준 시간대를 인스턴스화하기 위해 FindSystemTimeZoneById를 호출하지 않습니다. 대신 즉시 포함된 리소스 파일을 검색하여 FromSerializedString 메서드를 호출하기 전에 표준 시간대의 데이터가 포함된 문자열을 검색합니다.

코드 컴파일

이 예제에는 다음 사항이 필요합니다.

  • System.Windows.Forms.dll 및 System.Core.dll 대한 참조를 프로젝트에 추가할 수 있습니다.

  • 다음 네임스페이스를 가져올 수 있습니다.

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Reflection;
    using System.Resources;
    using System.Windows.Forms;
    
    Imports System.Globalization
    Imports System.IO
    Imports System.Reflection
    Imports System.Resources
    

참고 항목