ResourceManager.GetResourceSet(CultureInfo, Boolean, Boolean) 方法

定义

检索特定区域性的资源集合。Retrieves the resource set for a particular culture.

public:
 virtual System::Resources::ResourceSet ^ GetResourceSet(System::Globalization::CultureInfo ^ culture, bool createIfNotExists, bool tryParents);
public virtual System.Resources.ResourceSet GetResourceSet (System.Globalization.CultureInfo culture, bool createIfNotExists, bool tryParents);
abstract member GetResourceSet : System.Globalization.CultureInfo * bool * bool -> System.Resources.ResourceSet
override this.GetResourceSet : System.Globalization.CultureInfo * bool * bool -> System.Resources.ResourceSet
Public Overridable Function GetResourceSet (culture As CultureInfo, createIfNotExists As Boolean, tryParents As Boolean) As ResourceSet

参数

culture
CultureInfo

将要检索资源的区域性。The culture whose resources are to be retrieved.

createIfNotExists
Boolean

如果尚未加载,true 要加载资源集;否则为 falsetrue to load the resource set, if it has not been loaded yet; otherwise, false.

tryParents
Boolean

true 表示使用资源回退加载相应资源(如果找不到资源);false 表示绕过资源回退进程。true to use resource fallback to load an appropriate resource if the resource set cannot be found; false to bypass the resource fallback process.

返回

指定区域性的资源集。The resource set for the specified culture.

异常

culture 参数为 nullThe culture parameter is null.

tryParentstrue,未找到可用的资源集,并且没有默认区域性的资源。tryParents is true, no usable set of resources has been found, and there are no default culture resources.

示例

下面的示例调用GetResourceSet方法来检索法语(法国)区域性的区域性特定资源。The following example calls the GetResourceSet method to retrieve culture-specific resources for the French (France) culture. 然后枚举资源集中的所有资源。It then enumerates all of the resources in the resource set. 它包含名为 ShowNumbers 的可执行文件的源代码。It contains the source code for an executable named ShowNumbers.exe. 它还包括以下包含数字名称的两个文本文件。It also includes the following two text file that contain the names of numbers. 第一种是 NumberResources,其中包含英语中一到十个数字的名称:The first, NumberResources.txt, contains the names of numbers from one to ten in the English language:

  
one=one  
two=two  
three=three  
four=four  
five=five  
six=six  
seven=seven  
eight=eight  
nine=nine  
ten=ten  
  

第二个 NumberResources.fr-FR.TXT 包含法语中的数字的名称,采用法语:The second, NumberResources.fr-FR.txt, contains the names of numbers from one to four in the French language:

  
one=un  
two=deux  
three=trois  
four=quatre  
  

您可以使用批处理文件生成资源文件,将英语资源文件嵌入到可执行文件中,并为法语资源创建附属程序集。You can use a batch file to generate the resource files, embed the English language resource file into the executable, and create a satellite assembly for the French language resources. 下面是使用 Visual Basic 编译器生成可执行文件的批处理文件:Here's the batch file to generate an executable by using the Visual Basic compiler:

  
resgen NumberResources.txt  
vbc shownumbers.vb /resource:NumberResources.resources  
  
md fr-FR  
resgen NumberResources.fr-FR.txt  
al /out:fr-FR\ShowNumbers.resources.dll /culture:fr-FR /embed:NumberResources.fr-FR.resources  

对于C#编译器,可以使用以下批处理文件:For the C# compiler, you can use the following batch file:

  
resgen NumberResources.txt  
csc shownumbers.cs /resource:NumberResources.resources  
  
md fr-FR  
resgen NumberResources.fr-FR.txt  
al /out:fr-FR\ShowNumbers.resources.dll /culture:fr-FR /embed:NumberResources.fr-FR.resources  
  
using System;
using System.Globalization;
using System.Resources;

public class Example
{
   public static void Main()
   {
      String[] numbers = { "one", "two", "three", "four", "five", "six",
                           "seven", "eight", "nine", "ten" };
      var rm = new ResourceManager(typeof(NumberResources));
      ResourceSet rs = rm.GetResourceSet(CultureInfo.CreateSpecificCulture("fr-FR"),
                                         true, false);
      if (rs == null) {
         Console.WriteLine("No resource set.");
         return;
      }

      foreach (var number in numbers)
         Console.WriteLine("{0,-10} '{1}'", number + ":", rs.GetString(number));
   }
}

internal class NumberResources {}
// The example displays the following output:
//       one:       'un'
//       two:       'deux'
//       three:     'trois'
//       four:      'quatre'
//       five:      ''
//       six:       ''
//       seven:     ''
//       eight:     ''
//       nine:      ''
//       ten:       ''
Imports System.Globalization
Imports System.Resources

Module Example
   Public Sub Main()
      Dim numbers() As String = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" }
      Dim rm As New ResourceManager(GetType(NumberResources))
      Dim rs As ResourceSet = rm.GetResourceSet(CultureInfo.CreateSpecificCulture("fr-FR"), True, False)
      If rs Is Nothing Then Console.WriteLine("No resource set.") : Exit Sub
      For Each number In numbers
         Console.WriteLine("{0,-10} '{1}'", number + ":", rs.GetString(number))
      Next            
   End Sub
End Module

Public Class NumberResources
End Class
' The example displays the following output:
'       one:       'un'
'       two:       'deux'
'       three:     'trois'
'       four:      'quatre'
'       five:      ''
'       six:       ''
'       seven:     ''
'       eight:     ''
'       nine:      ''
'       ten:       ''

请注意,如果将createIfNotExists参数的值更改为false,则方法调用将返回null,因为资源管理器尚未加载法语资源。Note that if you change the value of the createIfNotExists argument to false, the method call returns null, since Resource Manager has not already loaded the French language resources.

注解

返回的资源集表示针对指定区域性进行了本地化的资源。The resource set that is returned represents the resources that are localized for the specified culture. 如果尚未为此区域性tryParents本地化资源, GetResourceSettrue使用资源回退规则加载相应的资源。If the resources have not been localized for that culture and tryParents is true, GetResourceSet uses resource fallback rules to load an appropriate resource. 如果tryParents nullfalse ,并且找不到特定于区域性的资源集,则该方法返回。If tryParents is false and a culture-specific resource set cannot be found, the method returns null. 有关资源回退的详细信息,请参阅打包和部署资源一文中的 "资源回退进程" 一节。For more information about resource fallback, see "The Resource Fallback Process" section in the Packaging and Deploying Resources article.

安全性

ReflectionPermission
当通过诸如InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])等机制后期绑定调用时。when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). 关联枚举:MemberAccessAssociated enumeration: MemberAccess.

适用于

另请参阅