ResourceManager クラス
アセンブリ: mscorlib (mscorlib.dll 内)


ResourceManager クラスは、カルチャ固有のリソースを検索し、ローカライズされたリソースが存在しない場合は代替となるリソースを提供し、リソースのシリアル化をサポートします。
ResourceManager のメソッドを使用すると、呼び出し元は GetObject メソッドと GetString メソッドを使用して、特定カルチャのリソースにアクセスできるようになります。既定では、これらのメソッドは、呼び出したスレッドの現在のカルチャ設定で決定されているカルチャに固有のリソースを返します。詳細については、Thread.CurrentUICulture のトピックを参照してください。呼び出し元で ResourceManager.GetResourceSet メソッドを使用すると、カルチャ フォールバック規則を無視し、特定のカルチャのリソースを表す ResourceSet を取得できます。取得した ResourceSet を使用して、そのカルチャ用にローカライズされているリソースに名前でアクセスできます。
理想的には、すべての言語のリソースを作成する必要があります。または、少なくとも 1 つ、言語のわかりやすいサブセットを作成する必要があります。リソース ファイル名は basename.cultureName.resources の名前付け規則に従います。basename は、アプリケーション名か、実行単位によって異なりますが、クラス名です。CultureInfo の Name プロパティを使用して cultureName が決定されます。ニュートラル カルチャ (InvariantCulture で返されます) に使用するリソースの名前は、basename.resources にする必要があります。
たとえば、アセンブリに複数のリソースがあり、これがリソース ファイルに格納されていて、リソース ファイルの basename が "MyResources" だと想定します。これらのリソース ファイルの名前は、"MyResources.ja-JP.resources"、"MyResources.de.resources"、"MyResources.zh-CHS.resources"、"MyResources.fr-BE.resources" などのようになり、日本語、ドイツ語、繁体字中国語、フランス語 (ベルギー) の各リソースが含まれます。既定のリソース ファイルの名前は MyResources.resources です。カルチャ固有のリソース ファイルは、通常、カルチャごとのサテライト アセンブリにパッケージ化されます。既定のリソース ファイルはメイン アセンブリに入れる必要があります。
この basename を持つリソースを表すために ResourceManager が作成されているとします。ResourceManager を使用して、"MyResources.ja-JP.resources" をカプセル化する ResourceSet を取得できます。この場合は、GetResourceSet(new CultureInfo ("ja-JP"), TRUE, FALSE) のようにメソッドを呼び出します。また、"MyResources" に "TOOLBAR_ICON" という名前のリソースが含まれていることがわかっている場合は、日本語にローカライズしたこのリソースの値を取得できます。この場合は、GetObject("TOOLBAR_ICON", new CultureInfo("ja-JP")) のようにメソッドを呼び出します。
ResourceManager のほとんどの基本的な用途では必ずしも必要ではありませんが、一般に出荷するアセンブリでは、SatelliteContractVersionAttribute を使用して、サテライトを再配置せずにメイン アセンブリのバージョン管理をサポートし、NeutralResourcesLanguageAttribute を使用して、存在しないサテライト アセンブリの検索を防止する必要があります。
サテライト アセンブリのバージョン管理サポートの詳細については、サテライト アセンブリに含まれるリソースの取得 のトピックを参照してください。サテライト アセンブリの作成の詳細については、「サテライト アセンブリの作成」を参照してください。Windows フォーム ダイアログのローカリゼーションの支援については、Microsoft .NET Framework SDK の「Windows フォーム リソース エディタ (Winres.exe)」ツールを参照してください。
リソースの設定と作成の詳細については、「アプリケーションのリソース」を参照してください。
![]() |
---|
<satelliteassemblies> ノードを作成する代わりに、ClickOnce 配置マニフェスト 機能を使用することもできます。 |
次のコード例に従って、構成ファイル セクションを作成します。
<?xml version ="1.0"?> <configuration> <satelliteassemblies> <assembly name="MainAssemblyName, Version=versionNumber, Culture=neutral, PublicKeyToken=null|yourPublicKeyToken"> <culture>cultureName1</culture> <culture>cultureName2</culture> <culture>cultureName3</culture> </assembly> </satelliteassemblies> </configuration>
-
配置するメイン アセンブリごとに、1 つ以上の <assembly> ノードを指定します。<assembly> ノードの属性は、アセンブリの完全修飾名を指定します。MainAssemblyName にメイン アセンブリの名前を指定し、メイン アセンブリに対応する Version、PublicKeyToken、Culture の各属性値を指定します。
Version 属性には、アセンブリのバージョン番号を指定します。たとえば、アセンブリの最初のリリースのバージョン番号として 1.0.0.0 を指定できます。
アセンブリに厳密な名前で署名していない場合、PublicKeyToken 属性にはキーワード "null" を指定します。アセンブリへの署名が済んでいる場合は、公開キー トークンを指定します。
Culture 属性には、メイン アセンブリを示すキーワード "neutral" を指定し、ResourceManager クラスが <culture> ノードに列挙されたカルチャだけを調査するようにします。
アセンブリの完全修飾名の詳細については、「アセンブリ名」を参照してください。厳密な名前付きアセンブリの詳細については、「厳密な名前付きアセンブリの作成と使用」を参照してください。
-
特定のカルチャ名 ("fr-FR" など) またはニュートラル カルチャ名 ("fr" など) で、<culture> ノードを少なくとも 1 つ指定します。
<satelliteassemblies> ノードに列挙されていないアセンブリでリソースが必要な場合、ResourceManager クラスは、標準の調査規則に従ってカルチャを調査します。

明示的なカルチャと現在の暗黙の UI カルチャを使用して、メイン アセンブリとサテライト アセンブリから文字列リソースを取得するコード例を次に示します。詳細については、「サテライト アセンブリの作成」のサブトピック「グローバル アセンブリ キャッシュ内にインストールしないサテライト アセンブリ用のディレクトリの位置」を参照してください。
' This code example demonstrates the ResourceManager() ' constructor and ResourceManager.GetString() method. Imports System Imports System.Resources Imports System.Reflection Imports System.Threading Imports System.Globalization ' 'Perform the following steps to use this code example: ' 'Main assembly: '1) In a main directory, create a file named "rmc.txt" that 'contains the following resource strings: ' 'day=Friday 'year=2006 'holiday="Cinco de Mayo" ' '2) Use the resgen.exe tool to generate the "rmc.resources" 'resource file from the "rmc.txt" input file. ' '> resgen rmc.txt ' 'Satellite Assembly: '3) Create a subdirectory of the main directory and name the 'subdirectory "es-ES", which is the culture name of the 'satellite assembly. ' '4) Create a file named "rmc.es-ES.txt" that contains the 'following resource strings: ' 'day=Viernes 'year=2006 'holiday="Cinco de Mayo" ' '5) Use the resgen.exe tool to generate the "rmc.es-ES.resources" 'resource file from the "rmc.es-ES.txt" input file. ' '> resgen rmc.es-ES.txt ' '6) Use the al.exe tool to create a satellite assembly. If the 'base name of the application is "rmc", the satellite assembly 'name must be "rmc.resources.dll". Also, specify the culture, 'which is es-ES. ' '> al /embed:rmc.es-ES.resources /c:es-ES /out:rmc.resources.dll ' '7) Assume the filename for this code example is "rmc.vb". Compile 'rmc.vb and embed the main assembly resource file, rmc.resources, in 'the executable assembly, rmc.exe: ' '>vbc /res:rmc.resources rmc.vb ' '8) Execute rmc.exe, which obtains and displays the embedded 'resource strings. ' Class Sample Public Shared Sub Main() Dim day As String Dim year As String Dim holiday As String Dim celebrate As String = "{0} will occur on {1} in {2}." & vbCrLf ' Create a resource manager. The GetExecutingAssembly() method ' gets rmc.exe as an Assembly object. Dim rm As New ResourceManager("rmc", [Assembly].GetExecutingAssembly()) ' Obtain resources using the current UI culture. Console.WriteLine("Obtain resources using the current UI culture.") ' Get the resource strings for the day, year, and holiday ' using the current UI culture. Use those strings to ' display a message. day = rm.GetString("day") year = rm.GetString("year") holiday = rm.GetString("holiday") Console.WriteLine(celebrate, holiday, day, year) ' Obtain the es-ES culture. Dim ci As New CultureInfo("es-ES") ' Get the resource strings for the day, year, and holiday ' using the specified culture. Use those strings to ' display a message. ' Obtain resources using the es-ES culture. Console.WriteLine("Obtain resources using the es-ES culture.") day = rm.GetString("day", ci) year = rm.GetString("year", ci) holiday = rm.GetString("holiday", ci) ' --------------------------------------------------------------- ' Alternatively, comment the preceding 3 code statements and ' uncomment the following 4 code statements: ' ---------------------------------------------------------------- ' Set the current UI culture to "es-ES" (Spanish-Spain). ' Thread.CurrentThread.CurrentUICulture = ci ' Get the resource strings for the day, year, and holiday ' using the current UI culture. Use those strings to ' display a message. ' day = rm.GetString("day") ' year = rm.GetString("year") ' holiday = rm.GetString("holiday") ' --------------------------------------------------------------- ' Regardless of the alternative that you choose, display a message ' using the retrieved resource strings. Console.WriteLine(celebrate, holiday, day, year) End Sub 'Main End Class 'Sample ' 'This code example produces the following results: ' '>rmc 'Obtain resources using the current UI culture. '"5th of May" will occur on Friday in 2006. ' 'Obtain resources using the es-ES culture. '"Cinco de Mayo" will occur on Viernes in 2006. '
// This code example demonstrates the ResourceManager() // constructor and ResourceManager.GetString() method. using System; using System.Resources; using System.Reflection; using System.Threading; using System.Globalization; /* Perform the following steps to use this code example: Main assembly: 1) In a main directory, create a file named "rmc.txt" that contains the following resource strings: day=Friday year=2006 holiday="Cinco de Mayo" 2) Use the resgen.exe tool to generate the "rmc.resources" resource file from the "rmc.txt" input file. > resgen rmc.txt Satellite Assembly: 3) Create a subdirectory of the main directory and name the subdirectory "es-ES", which is the culture name of the satellite assembly. 4) Create a file named "rmc.es-ES.txt" that contains the following resource strings: day=Viernes year=2006 holiday="Cinco de Mayo" 5) Use the resgen.exe tool to generate the "rmc.es-ES.resources" resource file from the "rmc.es-ES.txt" input file. > resgen rmc.es-ES.txt 6) Use the al.exe tool to create a satellite assembly. If the base name of the application is "rmc", the satellite assembly name must be "rmc.resources.dll". Also, specify the culture, which is es-ES. > al /embed:rmc.es-ES.resources /c:es-ES /out:rmc.resources.dll 7) Assume the filename for this code example is "rmc.cs". Compile rmc.cs and embed the main assembly resource file, rmc.resources, in the executable assembly, rmc.exe: >csc /res:rmc.resources rmc.cs 8) Execute rmc.exe, which obtains and displays the embedded resource strings. */ class Sample { public static void Main() { string day; string year; string holiday; string celebrate = "{0} will occur on {1} in {2}.\n"; // Create a resource manager. The GetExecutingAssembly() method // gets rmc.exe as an Assembly object. ResourceManager rm = new ResourceManager("rmc", Assembly.GetExecutingAssembly()); // Obtain resources using the current UI culture. Console.WriteLine("Obtain resources using the current UI culture."); // Get the resource strings for the day, year, and holiday // using the current UI culture. Use those strings to // display a message. day = rm.GetString("day"); year = rm.GetString("year"); holiday = rm.GetString("holiday"); Console.WriteLine(celebrate, holiday, day, year); // Obtain the es-ES culture. CultureInfo ci = new CultureInfo("es-ES"); // Get the resource strings for the day, year, and holiday // using the specified culture. Use those strings to // display a message. // Obtain resources using the es-ES culture. Console.WriteLine("Obtain resources using the es-ES culture."); day = rm.GetString("day", ci); year = rm.GetString("year", ci); holiday = rm.GetString("holiday", ci); // --------------------------------------------------------------- // Alternatively, comment the preceding 3 code statements and // uncomment the following 4 code statements: // ---------------------------------------------------------------- // Set the current UI culture to "es-ES" (Spanish-Spain). // Thread.CurrentThread.CurrentUICulture = ci; // Get the resource strings for the day, year, and holiday // using the current UI culture. Use those strings to // display a message. // day = rm.GetString("day"); // year = rm.GetString("year"); // holiday = rm.GetString("holiday"); // --------------------------------------------------------------- // Regardless of the alternative that you choose, display a message // using the retrieved resource strings. Console.WriteLine(celebrate, holiday, day, year); } } /* This code example produces the following results: >rmc Obtain resources using the current UI culture. "5th of May" will occur on Friday in 2006. Obtain resources using the es-ES culture. "Cinco de Mayo" will occur on Viernes in 2006. */

System.Resources.ResourceManager
System.ComponentModel.ComponentResourceManager


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- ResourceManager クラスのページへのリンク