ResourceSet.GetEnumerator 方法

定义

返回 IDictionaryEnumerator,它可以循环访问 ResourceSet

public:
 virtual System::Collections::IDictionaryEnumerator ^ GetEnumerator();
public virtual System.Collections.IDictionaryEnumerator GetEnumerator ();
[System.Runtime.InteropServices.ComVisible(false)]
public virtual System.Collections.IDictionaryEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
override this.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
override this.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
Public Overridable Function GetEnumerator () As IDictionaryEnumerator

返回

IDictionaryEnumeratorResourceSet

属性

例外

资源集已关闭或已释放。

示例

以下示例演示如何为文件 items.resources创建 ResourceSetrs 。 接下来,GetEnumerator使用 方法为 rs创建 IDictionaryEnumerator 。 循环 IDictionaryEnumerator 访问 rs 并将内容显示到控制台。

using namespace System;
using namespace System::Resources;
using namespace System::Collections;
int main()
{
   
   // Create a ResourceSet for the file items.resources.
   ResourceSet^ rs = gcnew ResourceSet( "items.resources" );
   
   // Create an IDictionaryEnumerator* to read the data in the ResourceSet.
   IDictionaryEnumerator^ id = rs->GetEnumerator();
   
   // Iterate through the ResourceSet and display the contents to the console.
   while ( id->MoveNext() )
      Console::WriteLine( "\n [{0}] \t {1}", id->Key, id->Value );

   rs->Close();
}
using System;
using System.Resources;
using System.Collections;

class EnumerateResources 
{
    public static void Main() 
    {
        // Create a ResourceSet for the file items.resources.
        ResourceSet rs = new ResourceSet("items.resources"); 

        // Create an IDictionaryEnumerator to read the data in the ResourceSet.
        IDictionaryEnumerator id = rs.GetEnumerator(); 

        // Iterate through the ResourceSet and display the contents to the console. 
        while(id.MoveNext())
          Console.WriteLine("\n[{0}] \t{1}", id.Key, id.Value); 

        rs.Close();
    }
}
Imports System.Resources
Imports System.Collections

Class EnumerateResources
   
   Public Shared Sub Main()
      ' Create a ResourceSet for the file items.resources.
      Dim rs As New ResourceSet("items.resources")      
      
      ' Create an IDictionaryEnumerator to read the data in the ResourceSet.
      Dim id As IDictionaryEnumerator = rs.GetEnumerator()
      
      ' Iterate through the ResourceSet and display the contents to the console. 
      While id.MoveNext()
         Console.WriteLine(ControlChars.NewLine + "[{0}] " + ControlChars.Tab + "{1}", id.Key, id.Value)
      End While 

      rs.Close()

   End Sub

End Class

注解

枚举器仅允许读取集合中的数据。 枚举器不能用于修改基础集合。

最初,枚举数定位在集合中第一个元素的前面。 Reset 也会将枚举器放回此位置。 在此位置,调用 Current 将引发异常。 因此,在读取 MoveNext 的值之前,必须调用 Current 将枚举器向前移动到集合的第一个元素。

在调用 CurrentMoveNext 之前,Reset 返回同一对象。 MoveNextCurrent 设置为下一个元素。

在传递到集合的末尾之后,枚举数放在集合中最后一个元素后面,且调用 MoveNext 会返回 false。 如果对 返回false的最后一MoveNext次调用,则调用 Current 将引发异常。 若要再次将 Current 设置为集合的第一个元素,可以调用 Reset 并接着调用 MoveNext

只要集合保持不变,枚举器就仍有效。 如果集合发生更改(如添加、修改或删除元素),则枚举器无效且无法恢复,对 MoveNextReset 的下一个调用将引发 InvalidOperationException。 如果在 MoveNextCurrent 之间修改集合,那么即使枚举数已经无效,Current 也将返回它所设置成的元素。

可以使用 IDictionaryEnumerator.Entry 属性访问存储在当前元素中的值。 IDictionaryEnumerator.Key使用 属性访问当前元素的键。 IDictionaryEnumerator.Value使用 属性访问当前元素的值。

枚举数没有对集合的独占访问权;因此,从头到尾对一个集合进行枚举在本质上不是一个线程安全的过程。 即使集合已同步,其他线程仍可能修改集合,这会导致枚举器引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。

适用于