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 的方法,调用方可使用 GetObjectGetString 两种方法访问特定区域性的资源。默认情况下,这些方法返回某区域性的资源,该区域性由执行调用的线程的当前区域性设置决定。(有关更多信息,请参见 Thread.CurrentUICulture。)调用方可使用 ResourceManager.GetResourceSet 方法获取表示特定区域性的资源的 ResourceSet,并忽略区域性代用规则。然后可以使用 ResourceSet 来按名称访问为该区域性本地化的资源。

理想情况下,应该为每种语言创建资源,或者至少为每种语言的有意义子集创建资源。资源文件名遵循“基名称.区域性名称.resources”的命名约定,其中基名称是应用程序名或类名(取决于期望的详细程度)。CultureInfoName 属性用来确定区域性名称。非特定语言区域性的资源(由 InvariantCulture 返回)应该命名为“基名称.resources”。

例如,假定某个程序集在基名称为 "MyResources" 的资源文件中有几个资源。这些资源文件的名称将为 "MyResources.ja-JP.resources""MyResources.de.resources""MyResources.zh-CHS.resources""MyResources.fr-BE.resources" 等,它们分别包含用于日语、德语、简体中文和法语(比利时)的资源。默认资源文件应该命名为 MyResources.resources。区域性特定的资源文件通常打包在每种区域性的附属程序集内。默认资源文件应该在主程序集内。

现在假定已创建 ResourceManager 来表示具有此基名称的资源。通过使用 ResourceManager,可以调用 GetResourceSet(new CultureInfo ("ja-JP"), TRUE, FALSE),从而获取封装 "MyResources.ja-JP.resources"ResourceSet。或者,如果已知 "MyResources" 包含名为 "TOOLBAR_ICON" 的资源,则可通过调用 GetObject("TOOLBAR_ICON", new CultureInfo("ja-JP")),获取针对日本进行了本地化的资源的值。

尽管不是 ResourceManager 的最基本用法所严格要求的,但是公开发布的程序集应使用 SatelliteContractVersionAttribute 来支持在不重新部署附属程序集的情况下控制主程序集的版本,并使用 NeutralResourcesLanguageAttribute 来避免查找可能根本不存在的附属程序集。

有关附属程序集版本支持的更多信息,请参见 检索附属程序集中的资源。若要了解有关创建附属程序集的更多信息,请参见 创建附属程序集。有关 Windows 窗体对话框的本地化方面的帮助,请参见 Microsoft .NET Framework SDK 中的 Windows 窗体资源编辑器 (Winres.exe) 工具。

若要了解有关设置和创建资源的更多信息,请参见 应用程序中的资源

警告

使用 ASP.NET 应用程序中的独立 .resources 文件将破坏 XCOPY 部署,这是由于资源在由 ReleaseAllResources 方法显式释放之前一直处于锁定状态。如果要将资源部署到 ASP.NET 应用程序中,则应该将 .resources 编译到附属程序集中。

警告

标记为私有的资源只有在它们所在的程序集中才是可访问的。因为附属程序集中不包含任何代码,所以通过任何机制都不能使用它的私有资源。因此,附属程序集中的资源应始终为公共的,以便可从主程序集中访问它们。嵌入到主程序集中的资源(无论是私有的还是公共的)对于主程序集来说都是可访问的。

<satelliteassemblies> 配置文件节点

您可以在应用程序配置文件中创建 <satelliteassemblies> 节点以指定您已为应用程序部署了一组特定的区域性设置,并且 ResourceManager 类不应尝试探测该节点中没有列出的任何区域性设置。

提示

创建 <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>

在您的配置文件中,执行以下操作:

  • 为部署的每个主程序集指定一个或多个 <assembly> 节点,其中 <assembly> 节点属性指定一个完全限定的程序集名称。指定主程序集的名称以代替 MainAssemblyName,并指定与主程序集对应的 VersionPublicKeyTokenCulture 属性值。

    对于 Version 属性,指定程序集的版本号。例如,程序集首次发布的版本号可能是 1.0.0.0。

    对于 PublicKeyToken 属性,如果没有使用强名称对程序集签名,则可以指定关键字“null”,如果已对程序集签名,则可以指定公钥标记。

    对于 Culture 属性,指定关键字“neutral”以指定主程序集并使 ResourceManager 类只探测 <culture> 节点中列出的区域性。

    有关完全限定程序集名称的更多信息,请参见 程序集名称。有关具有强名称的程序集的更多信息,请参见 创建和使用具有强名称的程序集

  • 使用特定的区域性名称(如“fr-FR”)或非特定区域性名称(如“fr”)指定一个或多个 <culture> 节点。

如果 <satelliteassemblies> 节点下未列出的任何程序集需要资源,则 ResourceManager 类使用标准探测规则探测区域性设置。

示例

下面的代码示例演示如何使用显式区域性和隐式当前 UI 区域性,从主程序集和附属程序集中获取字符串资源。有关更多信息,请参见 创建附属程序集 主题的副主题“Directory Locations for Satellite Assemblies Not Installed in the Global Assembly Cache”(未在全局程序集缓存中安装的附属程序集的目录位置)。

' 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

线程安全

该类型对于多线程操作是安全的。

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ResourceManager 成员
System.Resources 命名空间
CultureInfo
SatelliteContractVersionAttribute

其他资源

编码和本地化