ResourceReader.GetEnumerator 方法

定义

返回此 ResourceReader 对象的枚举器。

public:
 virtual System::Collections::IDictionaryEnumerator ^ GetEnumerator();
public:
 System::Collections::IDictionaryEnumerator ^ GetEnumerator();
public System.Collections.IDictionaryEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
override this.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
member this.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
Public Function GetEnumerator () As IDictionaryEnumerator

返回

ResourceReader 对象的枚举器。

实现

例外

读取器已关闭或释放,因此无法访问。

示例

本部分中的示例使用以下名为 PatientForm.txt .txt 文件来定义应用程序使用的资源。

Title="Top Pet Animal Clinic"
Label1="Patient Number:"
Label2="Pet Name:"
Label3="Species:"
Label4="Breed:"
Label5="Date of Birth:"
Label6="Age:"
Label7="Owner:"
Label8="Address:"
Label9="Home Phone:"
Label10="Work Phone:"
Label11="Mobile Phone:"

可以通过发出以下命令将 .txt 文件编译为 .resources 文件:

resgen PatientForm.txt

以下示例枚举 中的 PatientForm.resources 资源,并显示每个资源的名称和值。

using System;
using System.Collections;
using System.Resources;

public class Example
{
   public static void Main()
   {
      var rr = new ResourceReader("PatientForm.resources");
      IDictionaryEnumerator dict = rr.GetEnumerator();
      int ctr = 0;

      while (dict.MoveNext()) {
         ctr++;
         Console.WriteLine("{0:00}: {1} = {2}", ctr, dict.Key, dict.Value);
      }

      rr.Close();
   }
}
// The example displays the following output:
//       01: Label3 = "Species:"
//       02: Label2 = "Pet Name:"
//       03: Label1 = "Patient Number:"
//       04: Label7 = "Owner:"
//       05: Label6 = "Age:"
//       06: Label5 = "Date of Birth:"
//       07: Label4 = "Breed:"
//       08: Label9 = "Home Phone:"
//       09: Label8 = "Address:"
//       10: Title = "Top Pet Animal Clinic"
//       11: Label10 = "Work Phone:"
//       12: Label11 = "Mobile Phone:"
Imports System.Collections
Imports System.Resources

Module Example
   Public Sub Main()
      Dim rr As New ResourceReader("PatientForm.resources")
      Dim dict As IDictionaryEnumerator = rr.GetEnumerator
      Dim ctr As Integer

      Do While dict.MoveNext()
         ctr += 1
         Console.WriteLine("{0:00}: {1} = {2}", ctr, dict.Key, dict.Value)
      Loop
      
      rr.Close()
   End Sub
End Module
' The example displays the following output:
'       01: Label3 = "Species:"
'       02: Label2 = "Pet Name:"
'       03: Label1 = "Patient Number:"
'       04: Label7 = "Owner:"
'       05: Label6 = "Age:"
'       06: Label5 = "Date of Birth:"
'       07: Label4 = "Breed:"
'       08: Label9 = "Home Phone:"
'       09: Label8 = "Address:"
'       10: Title = "Top Pet Animal Clinic"
'       11: Label10 = "Work Phone:"
'       12: Label11 = "Mobile Phone:"

注解

通常,通过调用 方法枚举资源,GetEnumerator然后对返回IDictionaryEnumerator的对象重复调用 MoveNext 方法,直到方法返回 false。 资源名称可从 属性获取; IDictionaryEnumerator.Key 其值来自 IDictionaryEnumerator.Value 属性。 该示例演示了如何以这种方式枚举资源。

IDictionaryEnumerator.Value类实现 属性ResourceReader可能会引发以下异常:

可以通过调用 方法来处理异常, GetResourceData 以检索有关分配给命名资源的数据类型和字节数组的信息。 有关详细信息,请参阅类主题中的“使用 GetResourceData 按名称检索资源”部分 ResourceReader

重要

ResourceReader 包括两个返回枚举器的方法。 方法 GetEnumerator 返回接口 IDictionaryEnumerator 对象,是枚举资源时要调用的推荐方法。

适用于

另请参阅