ResourceManager クラスとは? わかりやすく解説

ResourceManager クラス

カルチャ固有のリソースに、実行時簡単にアクセスできるようにします。

名前空間: System.Resources
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class ResourceManager
Dim instance As ResourceManager
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class ResourceManager
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class ResourceManager
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class ResourceManager
SerializableAttribute 
ComVisibleAttribute(true) 
public class ResourceManager
解説解説

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>

構成ファイル内で次の作業行います

<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.Object
  System.Resources.ResourceManager
     System.ComponentModel.ComponentResourceManager
スレッド セーフスレッド セーフ

この型は、マルチスレッド操作に対して安全です。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ResourceManager クラス」の関連用語











ResourceManager クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ResourceManager クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS