ResourceReader.GetEnumerator 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳該 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.Value 屬性 ResourceReader 時,可能會拋出以下例外:
-
無法找到包含資料所屬類型類型的組合語言。
-
資料格式不符合預期。
-
無法找到資料所屬的類型。
你可以呼叫 GetResourceData 該方法來取得關於資料型別及指定資源的位元組陣列資訊來處理例外。 欲了解更多資訊,請參閱課程主題中的 ResourceReader 「使用 GetResourceData 按名稱檢索資源」部分。
這很重要
該 ResourceReader 類別包含兩種回傳枚舉器的方法。 該 GetEnumerator 方法回傳介面 IDictionaryEnumerator 物件,是列舉資源時建議呼叫的方法。