Поделиться через


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 в файл ресурсов, выполнив следующую команду:

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 метод, а затем многократно MoveNext вызывая метод для возвращаемого IDictionaryEnumerator объекта, пока метод не возвращается false. Имя ресурса доступно из IDictionaryEnumerator.Key свойства; его значение из IDictionaryEnumerator.Value свойства. В этом примере показано, как перечислить ресурсы таким образом.

Реализация IDictionaryEnumerator.Value свойства классом ResourceReader может вызвать следующие исключения:

  • FileNotFoundException

    Сборка, содержащая тип, к которому относятся данные, не удается найти.

  • FormatException

    Данные не в ожидаемом формате.

  • TypeLoadException

    Тип, к которому относятся данные, не удается найти.

Вы можете обработать исключение, вызвав GetResourceData метод для получения сведений о типе данных и массиве байтов, назначенных именованным ресурсом. Дополнительные сведения см. в разделе "Извлечение ресурсов по имени с помощью GetResourceData" в ResourceReader разделе класса.

Это важно

Класс ResourceReader включает два метода, возвращающих перечислители. Метод GetEnumerator возвращает IDictionaryEnumerator объект интерфейса и рекомендуется вызывать при перечислении ресурсов.

Применяется к

См. также раздел