Bewerken

Share via


ResXResourceSet Class

Definition

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
Inheritance
ResXResourceSet

Examples

The following example instantiates a ResXResourceSet object and illustrates how to enumerate its resources and retrieve individual resources by name. 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

The example calls a CreateResXFile method to create the necessary XML resource file. It requires a bitmap file named Logo.bmp in the directory in which the example is running.

Remarks

Important

Calling methods from this class with untrusted data is a security risk. Call the methods from this class only with trusted data. For more information, see Validate All Inputs.

The ResXResourceSet class enumerates over an IResourceReader, loads every name and value, and stores them in a hash table. You can then enumerate the resources in the ResXResourceSet object or retrieve individual resources by name.

A ResXResourceSet object provides a convenient way to read all the resources in a .resx file into memory. You can use the GetObject method to retrieve a particular resource when the .resx file has been read into a ResXResourceSet instance.

Notes to Inheritors

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.

Constructors

ResXResourceSet(Stream)

Initializes a new instance of the ResXResourceSet class using the system default ResXResourceReader to read resources from the specified stream.

ResXResourceSet(String)

Initializes a new instance of the ResXResourceSet class using the system default ResXResourceReader that opens and reads resources from the specified file.

Fields

Reader

Indicates the IResourceReader used to read the resources.

(Inherited from ResourceSet)
Table

The Hashtable in which the resources are stored.

(Inherited from ResourceSet)

Methods

Close()

Closes and releases any resources used by this ResourceSet.

(Inherited from ResourceSet)
Dispose()

Disposes of the resources (other than memory) used by the current instance of ResourceSet.

(Inherited from ResourceSet)
Dispose(Boolean)

Releases resources (other than memory) associated with the current instance, closing internal managed objects if requested.

(Inherited from ResourceSet)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetDefaultReader()

Returns the preferred resource reader class for this kind of ResXResourceSet.

GetDefaultWriter()

Returns the preferred resource writer class for this kind of ResXResourceSet.

GetEnumerator()

Returns an IDictionaryEnumerator that can iterate through the ResourceSet.

(Inherited from ResourceSet)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetObject(String)

Searches for a resource object with the specified name.

(Inherited from ResourceSet)
GetObject(String, Boolean)

Searches for a resource object with the specified name in a case-insensitive manner, if requested.

(Inherited from ResourceSet)
GetString(String)

Searches for a String resource with the specified name.

(Inherited from ResourceSet)
GetString(String, Boolean)

Searches for a String resource with the specified name in a case-insensitive manner, if requested.

(Inherited from ResourceSet)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ReadResources()

Reads all the resources and stores them in a Hashtable indicated in the Table property.

(Inherited from ResourceSet)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

IEnumerable.GetEnumerator()

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)

Extension Methods

Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

See also