ResourceReader.GetEnumerator 方法
定义
返回此 ResourceReader 对象的枚举器。Returns an enumerator for this ResourceReader object.
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 对象的枚举器。An enumerator for this ResourceReader object.
实现
例外
读取器已关闭或释放,因此无法访问。The reader has been closed or disposed, and cannot be accessed.
示例
本部分中的示例使用名为的以下 .txt 文件 PatientForm.txt 来定义应用程序使用的资源。The example in this section uses the following .txt file named PatientForm.txt to define the resources used by an application.
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 文件:You can compile the .txt file into a .resources file by issuing the following command:
resgen PatientForm.txtresgen PatientForm.txt
下面的示例枚举中的资源 PatientForm.resources ,并显示每个资源的名称和值。The following example enumerates the resources in PatientForm.resources and displays the name and value of each.
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 然后 MoveNext 在返回的对象上重复调用方法, IDictionaryEnumerator 直到该方法返回 false 。Typically, you enumerate resources by calling the GetEnumerator method and then repeatedly calling the MoveNext method on the returned IDictionaryEnumerator object until the method returns false. 资源名称可从属性获取, IDictionaryEnumerator.Key 其值来自 IDictionaryEnumerator.Value 属性。The resource name is available from the IDictionaryEnumerator.Key property; its value from the IDictionaryEnumerator.Value property. 该示例演示如何以这种方式枚举资源。The example illustrates how to enumerate resources in this way.
类的属性实现 IDictionaryEnumerator.Value ResourceReader 可能引发以下异常:The implementation of the IDictionaryEnumerator.Value property by the ResourceReader class can throw the following exceptions:
-
找不到包含数据所属的类型的程序集。The assembly that contains the type to which the data belongs cannot be found.
-
数据的格式不正确。The data is not in the expected format.
-
找不到数据所属的类型。The type to which the data belongs cannot be found.
您可以通过调用 GetResourceData 方法来检索有关数据类型的信息以及分配给该命名资源的字节数组的信息,从而处理此异常。You can handle the exception by calling the GetResourceData method to retrieve information about the data type and the byte array assigned to the named resource. 有关详细信息,请参阅类主题中的 "按名称使用 GetResourceData 检索资源" 部分 ResourceReader 。For more information, see the "Retrieving Resources by Name with GetResourceData" section in the ResourceReader class topic.
重要
ResourceReader类包含两个返回枚举器的方法。The ResourceReader class includes two methods that return enumerators. GetEnumerator方法返回 IDictionaryEnumerator 接口对象,是枚举资源时建议调用的方法。The GetEnumerator method returns an IDictionaryEnumerator interface object and is the recommended method to call when enumerating resources.