ResourceReader.GetEnumerator Método

Definição

Devolve um enumerador para este ResourceReader objeto.

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

Devoluções

Um enumerador para este ResourceReader objeto.

Implementações

Exceções

O leitor foi fechado ou descartado, e não pode ser acedido.

Exemplos

O exemplo nesta secção utiliza o seguinte ficheiro de .txt nomeado PatientForm.txt para definir os recursos usados por uma aplicação.

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:"

Pode compilar o ficheiro .txt num ficheiro .resources emitindo o seguinte comando:

resgen PatientForm.txt

O exemplo seguinte enumera os recursos presentes PatientForm.resources e mostra o nome e valor de cada um.

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:"

Observações

Normalmente, enumera recursos chamando o GetEnumerator método e depois chamando repetidamente o MoveNext método no objeto devolvido IDictionaryEnumerator até que o método retorne false. O nome do recurso está disponível na IDictionaryEnumerator.Key propriedade; o seu valor na IDictionaryEnumerator.Value propriedade. O exemplo ilustra como enumerar recursos desta forma.

A implementação da IDictionaryEnumerator.Value propriedade pela ResourceReader classe pode apresentar as seguintes exceções:

Pode tratar da exceção chamando o GetResourceData método para recuperar informações sobre o tipo de dados e o array de bytes atribuído ao recurso nomeado. Para mais informações, consulte a secção "Recuperar Recursos por Nome com o GetResourceData" no ResourceReader tópico da aula.

Importante

A ResourceReader aula inclui dois métodos que devolvem os enumeradores. O GetEnumerator método devolve um IDictionaryEnumerator objeto interface e é o método recomendado para chamar ao enumerar recursos.

Aplica-se a

Ver também