ResXResourceSet 類別

定義

表示 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
繼承
ResXResourceSet

範例

下列範例會具現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. 在執行此範例的目錄中, 它需要一個名為標誌 .bmp 的點陣圖檔案。It requires a bitmap file named Logo.bmp in the directory in which the example is running.

備註

類別會列舉IResourceReader、載入每個名稱和值, 並將它們儲存在雜湊表中。 ResXResourceSetThe 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.InheritanceDemandSecurityAction.LinkDemand套用至所有成員的類別層級包含和。This class contains a SecurityAction.LinkDemand and an SecurityAction.InheritanceDemand at the class level that applies to all members. 當立即呼叫端或衍生類別沒有完全信任許可權時, 就會擲回例外狀況(exception)。SecurityExceptionA 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. 當 .resx 檔案已GetObject讀入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)

使用系統預設的 ResXResourceReader 從指定資料流讀取資源,初始化 ResXResourceSet 類別的新執行個體。Initializes a new instance of the ResXResourceSet class using the system default ResXResourceReader to read resources from the specified stream.

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.

欄位

Reader

表示用來讀取資源的 IResourceReaderIndicates the IResourceReader used to read the resources.

(繼承來源 ResourceSet)
Table

用來儲存資源的 HashtableThe Hashtable in which the resources are stored.

(繼承來源 ResourceSet)

方法

Close()

關閉並釋放這個 ResourceSet 所使用的任何資源。Closes and releases any resources used by this ResourceSet.

(繼承來源 ResourceSet)
Dispose()

處置 ResourceSet 之目前執行個體所使用的資源 (除了記憶體之外)。Disposes of the resources (other than memory) used by the current instance of ResourceSet.

(繼承來源 ResourceSet)
Dispose(Boolean)

釋放與目前執行個體關聯的資源 (除了記憶體以外),如果要求的話,請關閉內部 Managed 物件。Releases resources (other than memory) associated with the current instance, closing internal managed objects if requested.

(繼承來源 ResourceSet)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetDefaultReader()

傳回這種 ResXResourceSet 較佳的資源讀取器類別。Returns the preferred resource reader class for this kind of ResXResourceSet.

GetDefaultWriter()

傳回這種 ResXResourceSet 較佳的資源寫入器類別。Returns the preferred resource writer class for this kind of ResXResourceSet.

GetEnumerator()

傳回可以逐一查看 IDictionaryEnumeratorResourceSetReturns an IDictionaryEnumerator that can iterate through the ResourceSet.

(繼承來源 ResourceSet)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetObject(String)

以指定的名稱搜尋資源物件。Searches for a resource object with the specified name.

(繼承來源 ResourceSet)
GetObject(String, Boolean)

如果要求的話,以不區分大小寫的指定名稱搜尋資源物件。Searches for a resource object with the specified name in a case-insensitive manner, if requested.

(繼承來源 ResourceSet)
GetString(String)

以指定的名稱搜尋 String 資源。Searches for a String resource with the specified name.

(繼承來源 ResourceSet)
GetString(String, Boolean)

如果要求的話,以不區分大小寫的指定名稱搜尋 String 資源。Searches for a String resource with the specified name in a case-insensitive manner, if requested.

(繼承來源 ResourceSet)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ReadResources()

讀取所有資源,並將它們儲存在 Hashtable 屬性指示的 Table 中。Reads all the resources and stores them in a Hashtable indicated in the Table property.

(繼承來源 ResourceSet)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

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.

(繼承來源 ResourceSet)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

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.

適用於

另請參閱