ResXResourceSet 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示 XML 资源 (.resx) 文件中的所有资源。
public ref class ResXResourceSet : System::Resources::ResourceSetpublic class ResXResourceSet : System.Resources.ResourceSettype ResXResourceSet = class
    inherit ResourceSetPublic Class ResXResourceSet
Inherits ResourceSet- 继承
示例
以下示例实例化对象 ResXResourceSet ,并演示如何枚举其资源并按名称检索单个资源。 对于它枚举的每个资源,该示例在调用 GetString 或 GetObject 方法时使用 IDictionaryEnumerator.Key 属性,具体取决于资源的值是字符串还是对象。
using System;
using System.Collections;
using System.Drawing;
using System.Resources;
public class Example
{
   public static void Main()
   {
      CreateResXFile();
      
      ResXResourceSet resSet = new ResXResourceSet(@".\StoreResources.resx");
      IDictionaryEnumerator dict = resSet.GetEnumerator();
      while (dict.MoveNext()) {
         string key = (string) dict.Key;
         // Retrieve resource by name.
         if (dict.Value is string)
            Console.WriteLine("{0}: {1}", key, resSet.GetString(key));
         else
            Console.WriteLine("{0}: {1}", key, resSet.GetObject(key));   
      }
   }
   private static void CreateResXFile()
   {
      Bitmap logo = new Bitmap(@".\Logo.bmp");
      ResXDataNode node;
      
      ResXResourceWriter rw = new ResXResourceWriter(@".\StoreResources.resx");
      node = new ResXDataNode("Logo", logo);
      node.Comment = "The corporate logo.";
      rw.AddResource(node); 
      rw.AddResource("AppTitle", "Store Locations");
      node = new ResXDataNode("nColumns", 5);
      node.Comment = "The number of columns in the Store Location table";
      rw.AddResource(node);
      rw.AddResource("City", "City");
      rw.AddResource("State", "State");
      rw.AddResource("Code", "Zip Code");
      rw.AddResource("Telephone", "Phone");
      rw.Generate();
      rw.Close();
   }
}
// The example displays the following output:
//       Telephone: Phone
//       Code: Zip Code
//       State: State
//       City: City
//       nColumns: 5
//       AppTitle: Store Locations
//       Logo: System.Drawing.Bitmap
Imports System.Collections
Imports System.Drawing
Imports System.Resources
Module Example
   Public Sub Main()
      CreateResXFile()
      
      Dim resSet As New ResXResourceSet(".\StoreResources.resx")
      Dim dict As IDictionaryEnumerator = resSet.GetEnumerator()
      Do While dict.MoveNext()
         Dim key As String = CStr(dict.Key)
         ' Retrieve resource by name.
         If typeof dict.Value Is String Then
            Console.WriteLine("{0}: {1}", key, resSet.GetString(key))
         Else
            Console.WriteLine("{0}: {1}", key, resSet.GetObject(key))   
         End If
      Loop
   End Sub
   
   Private Sub CreateResXFile()
      Dim logo As New Bitmap(".\Logo.bmp")
      Dim node As ResXDataNode
      
      Dim rw As New ResXResourceWriter(".\StoreResources.resx")
      node = New ResXDataNode("Logo", logo)
      node.Comment = "The corporate logo."
      rw.AddResource(node) 
      rw.AddResource("AppTitle", "Store Locations")
      node = New ResXDataNode("nColumns", 5)
      node.Comment = "The number of columns in the Store Location table"
      rw.AddResource(node)
      rw.AddResource("City", "City")
      rw.AddResource("State", "State")
      rw.AddResource("Code", "Zip Code")
      rw.AddResource("Telephone", "Phone")
      rw.Generate()
      rw.Close()
   End Sub
End Module
' The example displays the following output:
'       Telephone: Phone
'       Code: Zip Code
'       State: State
'       City: City
'       nColumns: 5
'       AppTitle: Store Locations
'       Logo: System.Drawing.Bitmap
该示例调用 方法 CreateResXFile 以创建必要的 XML 资源文件。 它要求在运行示例的目录中 Logo.bmp 一个名为 的位图文件。
注解
重要
使用不受信任的数据调用此类中的方法存在安全风险。 仅使用受信任的数据调用此类中的方法。 有关详细信息,请参阅 验证所有输入。
类 ResXResourceSet 枚举 上的 IResourceReader,加载每个名称和值,并将其存储在哈希表中。 然后,可以枚举 对象中的资源或 ResXResourceSet 按名称检索单个资源。
对象 ResXResourceSet 提供了一种将 .resx 文件中的所有资源读取到内存中的便捷方法。 当 .resx 文件已读入ResXResourceSet实例时,可以使用 GetObject 方法检索特定资源。
继承者说明
使用其自己的资源读取器和编写器的 派生类 ResXResourceSet 应重写 GetDefaultReader() 和 GetDefaultWriter() 方法,以提供用于解释实例的适当 ResXResourceSet 功能。
构造函数
| ResXResourceSet(Stream) | 使用从所指定流读取资源的系统默认的 ResXResourceReader 来初始化 ResXResourceSet 类的新实例。 | 
| ResXResourceSet(String) | 使用从指定文件打开和读取资源的系统默认值ResXResourceReader初始化 类的新实例ResXResourceSet。 | 
字段
| Reader | 指示用于读取资源的 IResourceReader。(继承自 ResourceSet) | 
| Table | 存储资源的 Hashtable。(继承自 ResourceSet) | 
方法
| Close() | 通过此 ResourceSet 来关闭和释放所有资源。(继承自 ResourceSet) | 
| Dispose() | 处置由 ResourceSet 的当前实例使用的资源(内存除外)。(继承自 ResourceSet) | 
| Dispose(Boolean) | 释放与当前实例关联的资源(内存除外),并关闭内部托管对象(如果请求这样做)。(继承自 ResourceSet) | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetDefaultReader() | 返回此类 ResXResourceSet 的首选资源读取器类。 | 
| GetDefaultWriter() | 返回此类 ResXResourceSet 的首选资源编写器类。 | 
| GetEnumerator() | 返回 IDictionaryEnumerator,它可以循环访问 ResourceSet。(继承自 ResourceSet) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetObject(String) | 搜索具有指定名称的资源对象。(继承自 ResourceSet) | 
| GetObject(String, Boolean) | 如果请求的话,按照不区分大小写的方式搜索具有指定名称的资源对象。(继承自 ResourceSet) | 
| GetString(String) | 搜索具有指定名称的 String 资源。(继承自 ResourceSet) | 
| GetString(String, Boolean) | 如果请求的话,按照不区分大小写的方式搜索具有指定名称的 String 资源。(继承自 ResourceSet) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ReadResources() | 读取所有资源,并将它们存储在 Hashtable 属性中指示的 Table 中。(继承自 ResourceSet) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| IEnumerable.GetEnumerator() | 返回一个 IEnumerator 对象,以避免与  | 
扩展方法
| Cast<TResult>(IEnumerable) | 将 IEnumerable 的元素强制转换为指定的类型。 | 
| OfType<TResult>(IEnumerable) | 根据指定类型筛选 IEnumerable 的元素。 | 
| AsParallel(IEnumerable) | 启用查询的并行化。 | 
| AsQueryable(IEnumerable) | 将 IEnumerable 转换为 IQueryable。 |