ResourceSet.GetEnumerator 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一个可循环访问 ResourceSet的 IDictionaryEnumerator。
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
	返回
此 ResourceSet的 IDictionaryEnumerator。
- 属性
 
例外
资源集已关闭或释放。
示例
以下示例演示如何为文件 items.resources创建 ResourceSetrs。 接下来,GetEnumerator 方法用于为 rs创建 IDictionaryEnumerator。 
              IDictionaryEnumerator 循环访问 rs,并向控制台显示内容。
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 将引发异常。 因此,在读取 Current值之前,必须调用 MoveNext 将枚举器提升到集合的第一个元素。
Current 返回相同的对象,直到调用 MoveNext 或 Reset。 MoveNext 将 Current 设置为下一个元素。
传递集合末尾后,枚举器位于集合中的最后一个元素之后,调用 MoveNext 返回 false。 如果上次调用 MoveNext 返回 false,则调用 Current 将引发异常。 若要再次将 Current 设置为集合的第一个元素,可以调用 Reset 后跟 MoveNext。
只要集合保持不变,枚举器就保持有效。 如果对集合进行了更改(如添加、修改或删除元素),则枚举器将不可恢复地失效,并且对 MoveNext 或 Reset 的下一次调用将引发 InvalidOperationException。 如果集合在 MoveNext 和 Current之间修改,则 Current 将返回它设置为的元素,即使枚举器已失效。
可以使用 IDictionaryEnumerator.Entry 属性访问存储在当前元素中的值。 使用 IDictionaryEnumerator.Key 属性访问当前元素的键。 使用 IDictionaryEnumerator.Value 属性访问当前元素的值。
枚举器不具有对集合的独占访问权限;因此,通过集合进行枚举本质上不是线程安全的过程。 即使集合同步,其他线程仍可以修改集合,这会导致枚举器引发异常。 若要保证枚举期间的线程安全性,可以在整个枚举期间锁定集合,也可以捕获由其他线程所做的更改导致的异常。