ResXResourceSet ResXResourceSet ResXResourceSet ResXResourceSet Class

定义

表示 XML 资源 (.resx) 文件中的所有资源。Represents all resources in an XML resource (.resx) file.

public ref class ResXResourceSet : System::Resources::ResourceSet
public class ResXResourceSet : System.Resources.ResourceSet
type ResXResourceSet = class
    inherit ResourceSet
Public Class ResXResourceSet
Inherits ResourceSet
继承
ResXResourceSetResXResourceSetResXResourceSetResXResourceSet

示例

下面的示例实例化ResXResourceSet对象,并说明了如何枚举其资源和按名称检索各个资源。The following example instantiates a ResXResourceSet object and illustrates how to enumerate its resources and retrieve individual resources by name. 对于它枚举每个资源,该示例使用IDictionaryEnumerator.Key对的调用中的属性GetStringGetObject方法,具体取决于资源的值是字符串或对象。For each resource that it enumerates, the example uses the IDictionaryEnumerator.Key property in a call to the GetString or GetObject method, depending on whether the value of the resource is a string or an object.

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 资源文件。The example calls a CreateResXFile method to create the necessary XML resource file. 它需要在其中运行该示例的目录中名为 Logo.bmp 位图文件。It requires a bitmap file named Logo.bmp in the directory in which the example is running.

注解

ResXResourceSet类枚举IResourceReader、 加载的每个名称和值,并将其存储在哈希表。The ResXResourceSet class enumerates over an IResourceReader, loads every name and value, and stores them in a hash table. 然后,可以枚举中的资源ResXResourceSet对象,或按名称检索各个资源。You can then enumerate the resources in the ResXResourceSet object or retrieve individual resources by name.

备注

此类包含SecurityAction.LinkDemand和一个SecurityAction.InheritanceDemand在类级别应用于所有成员。This class contains a SecurityAction.LinkDemand and an SecurityAction.InheritanceDemand at the class level that applies to all members. 一个SecurityException直接调用方或派生的类没有完全信任权限时将引发异常。A SecurityException exception is thrown when the immediate caller or a derived class does not have full-trust permission.

一个ResXResourceSet对象提供了方便地读取到内存中的.resx 文件中的所有资源。A ResXResourceSet object provides a convenient way to read all the resources in a .resx file into memory. 可以使用GetObject方法来检索特定资源的.resx 文件读入时ResXResourceSet实例。You can use the GetObject method to retrieve a particular resource when the .resx file has been read into a ResXResourceSet instance.

继承者说明

派生类的ResXResourceSet使用其自己的资源读取器和编写器应重写GetDefaultReader()GetDefaultWriter()方法,以提供适当的功能,用于解释ResXResourceSet实例。Derived classes of ResXResourceSet that use their own resource reader and writer should override the GetDefaultReader() and GetDefaultWriter() methods to provide the appropriate functionality for interpreting the ResXResourceSet instance.

构造函数

ResXResourceSet(Stream) ResXResourceSet(Stream) ResXResourceSet(Stream) ResXResourceSet(Stream)

使用从所指定流读取资源的系统默认的 ResXResourceReader 来初始化 ResXResourceSet 类的新实例。Initializes a new instance of the ResXResourceSet class using the system default ResXResourceReader to read resources from the specified stream.

ResXResourceSet(String) ResXResourceSet(String) ResXResourceSet(String) ResXResourceSet(String)

使用从指定文件打开并读取资源的系统默认的 ResXResourceReader 来初始化 ResXResourceSet 类的新实例。Initializes a new instance of a ResXResourceSet class using the system default ResXResourceReader that opens and reads resources from the specified file.

方法

Close() Close() Close() Close()

通过此 ResourceSet 来关闭和释放所有资源。Closes and releases any resources used by this ResourceSet.

(Inherited from ResourceSet)
Dispose() Dispose() Dispose() Dispose()

处置由 ResourceSet 的当前实例使用的资源(内存除外)。Disposes of the resources (other than memory) used by the current instance of ResourceSet.

(Inherited from ResourceSet)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

释放与当前实例关联的资源(内存除外),并关闭内部托管对象(如果请求这样做)。Releases resources (other than memory) associated with the current instance, closing internal managed objects if requested.

(Inherited from ResourceSet)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetDefaultReader() GetDefaultReader() GetDefaultReader() GetDefaultReader()

返回此类 ResXResourceSet 的首选资源读取器类。Returns the preferred resource reader class for this kind of ResXResourceSet.

GetDefaultWriter() GetDefaultWriter() GetDefaultWriter() GetDefaultWriter()

返回此类 ResXResourceSet 的首选资源编写器类。Returns the preferred resource writer class for this kind of ResXResourceSet.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

返回 IDictionaryEnumerator,它可以循环访问 ResourceSetReturns an IDictionaryEnumerator that can iterate through the ResourceSet.

(Inherited from ResourceSet)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetObject(String) GetObject(String) GetObject(String) GetObject(String)

搜索具有指定名称的资源对象。Searches for a resource object with the specified name.

(Inherited from ResourceSet)
GetObject(String, Boolean) GetObject(String, Boolean) GetObject(String, Boolean) GetObject(String, Boolean)

如果请求的话,按照不区分大小写的方式搜索具有指定名称的资源对象。Searches for a resource object with the specified name in a case-insensitive manner, if requested.

(Inherited from ResourceSet)
GetString(String) GetString(String) GetString(String) GetString(String)

搜索具有指定名称的 String 资源。Searches for a String resource with the specified name.

(Inherited from ResourceSet)
GetString(String, Boolean) GetString(String, Boolean) GetString(String, Boolean) GetString(String, Boolean)

如果请求的话,按照不区分大小写的方式搜索具有指定名称的 String 资源。Searches for a String resource with the specified name in a case-insensitive manner, if requested.

(Inherited from ResourceSet)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ReadResources() ReadResources() ReadResources() ReadResources()

读取所有资源,并将它们存储在 Hashtable 属性中指示的 Table 中。Reads all the resources and stores them in a Hashtable indicated in the Table property.

(Inherited from ResourceSet)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

字段

Reader Reader Reader Reader

指示用于读取资源的 IResourceReaderIndicates the IResourceReader used to read the resources.

(Inherited from ResourceSet)
Table Table Table Table

存储资源的 HashtableThe Hashtable in which the resources are stored.

(Inherited from ResourceSet)

显式界面实现

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

返回一个 IEnumerator 对象,以避免与 Dispose 之间出现争用情况。Returns an IEnumerator object to avoid a race condition with Dispose. 不应直接在您的代码中使用此成员。This member is not intended to be used directly from your code.

(Inherited from ResourceSet)

扩展方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

安全性

LinkDemand
对于直接调用方的完全信任。for full trust for the immediate caller. 部分受信任的代码不能使用此类。This class cannot be used by partially trusted code.

InheritanceDemand
对于直接调用方的完全信任。for full trust for the immediate caller. 不能由部分受信任代码继承此类。This class cannot be inherited by partially trusted code.

适用于

另请参阅