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 的列舉值。

實作

例外狀況

讀取器已關閉或已處置,因此無法存取。

範例

本節中的範例會使用名為 的下列 .txt 檔案 PatientForm.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.ValueResourceReader 屬性實作可能會擲回下列例外狀況:

您可以呼叫 GetResourceData 方法來擷取指派給具名資源之數據類型和位元組陣列的相關信息,以處理例外狀況。 For more information, see the "Retrieving Resources by Name with GetResourceData" section in the ResourceReader class topic.

重要

類別 ResourceReader 包含兩個傳回列舉值的方法。 方法 GetEnumerator 會傳 IDictionaryEnumerator 回介面物件,而且是列舉資源時所要呼叫的建議方法。

適用於

另請參閱