Share via


如何:从嵌入的资源还原时区

本主题介绍如何还原已保存在资源文件中的时区。 有关保存时区的信息和说明,请参见如何:将时区保存到嵌入的资源中

从嵌入的资源中反序列化 TimeZoneInfo 对象

  1. 如果要检索的时区不是自定义时区,请尝试使用 FindSystemTimeZoneById 方法将其实例化。

  2. 通过传递嵌入的资源文件的完全限定名以及对包含该资源文件的程序集的引用,可以实例化 ResourceManager 对象。

    如果无法确定嵌入的资源文件的完全限定名,请使用 Ildasm.exe(MSIL 反汇编程序) 检查程序集的清单。 清单中的 .mresource 项用于标识资源。 在该示例中,资源的完全限定名为 SerializeTimeZoneData.SerializedTimeZones。

    如果嵌入资源文件的程序集与包含时区实例化代码的程序集相同,则可以通过调用 static(在 Visual Basic 中为 Shared)GetExecutingAssembly 方法来检索对该资源文件的引用。

  3. 如果调用 FindSystemTimeZoneById 方法失败,或是要实例化自定义时区,请通过调用 ResourceManager.GetString 方法来检索包含该序列化时区的字符串。

  4. 通过调用 FromSerializedString 方法可反序列化时区数据。

示例

下面的示例对嵌入的 .NET XML 资源文件中存储的 TimeZoneInfo 对象执行反序列化。

Private Sub DeserializeTimeZones()
   Dim cst, palmer As TimeZoneInfo
   Dim timeZoneString As String
   Dim resMgr As ResourceManager = New ResourceManager("SerializeTimeZoneData.SerializedTimeZones", Assembly.GetExecutingAssembly)

   ' 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
private void DeserializeTimeZones()
{
   TimeZoneInfo cst, palmer;
   string timeZoneString;
   ResourceManager resMgr = new ResourceManager("SerializeTimeZoneData.SerializedTimeZones", Assembly.GetExecutingAssembly());

   // 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;
   }
}

此代码演示如何进行异常处理,以确保存在应用程序所需的 TimeZoneInfo 对象。 它首先使用 FindSystemTimeZoneById 方法从注册表中检索 TimeZoneInfo 对象,从而来尝试实例化该对象。 如果无法实例化时区,该代码则从嵌入的资源文件中检索该时区。

由于自定义时区(使用 CreateCustomTimeZone 方法实例化的时区)的数据并不存储在注册表中,因此该代码并未调用 FindSystemTimeZoneById 来实例化南极帕默时区, 而是在调用 FromSerializedString 方法之前立即检索嵌入的资源文件中包含该时区数据的字符串。

编译代码

此示例需要:

  • 在项目中添加一个对 System.Windows.Forms.dll 和 System.Core.dll 的引用。

  • 导入下列命名空间:

    Imports System.Globalization
    Imports System.IO
    Imports System.Reflection
    Imports System.Resources
    
    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;
    

请参见

任务

如何:将时区保存到嵌入的资源中

概念

时区概述

其他资源

日期、时间和时区