ResourceManager.GetObject 方法

定义

返回为当前区域性的指定非字符串资源的值。Returns the value of the specified non-string resource for the current culture.

重载

GetObject(String)

返回指定的非字符串资源的值。Returns the value of the specified non-string resource.

GetObject(String, CultureInfo)

获取为指定区域性本地化的指定非字符串资源的值。Gets the value of the specified non-string resource localized for the specified culture.

GetObject(String)

返回指定的非字符串资源的值。Returns the value of the specified non-string resource.

public:
 virtual System::Object ^ GetObject(System::String ^ name);
public virtual object GetObject (string name);
abstract member GetObject : string -> obj
override this.GetObject : string -> obj
Public Overridable Function GetObject (name As String) As Object

参数

name
String

要获取的资源名。The name of the resource to get.

返回

针对调用方的当前区域性设置而本地化的资源的值。The value of the resource localized for the caller's current culture settings. 如果相应的资源集存在,但无法找到 name,该方法返回 nullIf an appropriate resource set exists but name cannot be found, the method returns null.

异常

name 参数为 nullThe name parameter is null.

未找到可用的本地资源集,并且没有默认区域性资源。No usable set of localized resources has been found, and there are no default culture resources. 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

默认区域性的资源位于无法找到的附属程序集。The default culture's resources reside in a satellite assembly that could not be found. 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

示例

下面的示例使用 GetObject(String) 方法反序列化一个自定义对象。The following example uses the GetObject(String) method to deserialize a custom object. 该示例包含一个名为 UIElements.cs (UIElements)的源代码文件,如果使用 Visual Basic)来定义以下名为PersonTable的结构。The example includes a source code file named UIElements.cs (UIElements.vb if you're using Visual Basic) that defines the following structure named PersonTable. 此结构应由显示表列的本地化名称的常规表显示例程使用。This structure is intended to be used by a general table display routine that displays the localized names of table columns. 请注意, PersonTable 结构标有 SerializableAttribute 属性。Note that the PersonTable structure is marked with the SerializableAttribute attribute.

using System;

[Serializable] public struct PersonTable
{
   public readonly int nColumns;
   public readonly string column1; 
   public readonly string column2;
   public readonly string column3; 
   public readonly int width1;
   public readonly int width2;
   public readonly int width3;
   
   public PersonTable(string column1, string column2, string column3,
                  int width1, int width2, int width3)
   {
      this.column1 = column1;
      this.column2 = column2;
      this.column3 = column3;
      this.width1 = width1;
      this.width2 = width2;
      this.width3 = width3;
      this.nColumns = typeof(PersonTable).GetFields().Length / 2; 
   }
}
<Serializable> Public Structure PersonTable
   Public ReadOnly nColumns As Integer
   Public Readonly column1 As String
   Public ReadOnly column2 As String
   Public ReadOnly column3 As String
   Public ReadOnly width1 As Integer
   Public ReadOnly width2 As Integer
   Public ReadOnly width3 As Integer
   
   Public Sub New(column1 As String, column2 As String, column3 As String,
                  width1 As Integer, width2 As Integer, width3 As Integer)
      Me.column1 = column1
      Me.column2 = column2
      Me.column3 = column3
      Me.width1 = width1
      Me.width2 = width2
      Me.width3 = width3
      Me.nColumns = Me.GetType().GetFields().Count \ 2 
   End Sub
End Structure

下面的代码来自名为 CreateResources.cs(对于 Visual Basic 则为 CreateResources.vb)的文件,该代码创建一个名为 UIResources.resx 的 XML 资源文件,该文件存储有表标题和包含已针对英语语言本地化的应用的信息的 PersonTable 对象。The following code from a file named CreateResources.cs (CreateResources.vb for Visual Basic) creates an XML resource file named UIResources.resx that stores a table title and a PersonTable object that contains information for an app that is localized for the English language.

using System;
using System.Resources;

public class CreateResource
{
   public static void Main()
   {
      PersonTable table = new PersonTable("Name", "Employee Number", 
                                          "Age", 30, 18, 5);
      ResXResourceWriter rr = new ResXResourceWriter(@".\UIResources.resx");
      rr.AddResource("TableName", "Employees of Acme Corporation");
      rr.AddResource("Employees", table);
      rr.Generate();
      rr.Close();
   }
}
Imports System.Resources

Module CreateResource
   Public Sub Main()
      Dim table As New PersonTable("Name", "Employee Number", "Age", 30, 18, 5)
      Dim rr As New ResXResourceWriter(".\UIResources.resx")
      rr.AddResource("TableName", "Employees of Acme Corporation")
      rr.AddResource("Employees", table)
      rr.Generate()
      rr.Close()
   End Sub
End Module

下面的代码位于名为 GetObject.cs (GetObject.vb) 的源代码文件中,然后检索资源并将其显示在控制台上。The following code in a source code file named GetObject.cs (GetObject.vb) then retrieves the resources and displays them to the console.

using System;
using System.Resources;

[assembly: NeutralResourcesLanguageAttribute("en")]

public class Example
{
   public static void Main()
   {
      string fmtString = String.Empty;
      ResourceManager rm = new ResourceManager("UIResources", typeof(Example).Assembly);       
      string title = rm.GetString("TableName");
      PersonTable tableInfo = (PersonTable) rm.GetObject("Employees");

      if (! String.IsNullOrEmpty(title)) {
         fmtString = "{0," + ((Console.WindowWidth + title.Length) / 2).ToString() + "}"; 
         Console.WriteLine(fmtString, title);      
         Console.WriteLine();
      }

      for (int ctr = 1; ctr <= tableInfo.nColumns; ctr++) {
         string columnName = "column"  + ctr.ToString();
         string widthName = "width" + ctr.ToString();
         string value = tableInfo.GetType().GetField(columnName).GetValue(tableInfo).ToString();
         int width = (int) tableInfo.GetType().GetField(widthName).GetValue(tableInfo);
         fmtString = "{0,-" + width.ToString() + "}";
         Console.Write(fmtString, value);
      }      
      Console.WriteLine();
   }
}
Imports System.Resources

<Assembly: NeutralResourcesLanguageAttribute("en")>

Module Example
   Public Sub Main()
      Dim fmtString As String = String.Empty
      Dim rm As New ResourceManager("UIResources", GetType(Example).Assembly)       
      Dim title As String = rm.GetString("TableName")
      Dim tableInfo As PersonTable = DirectCast(rm.GetObject("Employees"), PersonTable)

      If Not String.IsNullOrEmpty(title) Then
         fmtString = "{0," + ((Console.WindowWidth + title.Length) \ 2).ToString() + "}" 
         Console.WriteLine(fmtString, title)      
         Console.WriteLine()
      End If

      For ctr As Integer = 1 To tableInfo.nColumns
         Dim columnName As String = "column"  + ctr.ToString()
         Dim widthName As String = "width" + ctr.ToString()
         Dim value As String = CStr(tableInfo.GetType().GetField(columnName).GetValue(tableInfo))
         Dim width As Integer = CInt(tableInfo.GetType().GetField(widthName).GetValue(tableInfo))
         fmtString = "{0,-" + width.ToString() + "}"
         Console.Write(fmtString, value)
      Next      
      Console.WriteLine()
   End Sub
End Module

可以生成必要的资源文件和程序集,并通过执行以下批处理文件运行该应用。You can build the necessary resource file and assemblies and run the app by executing the following batch file. 必须使用 /r 选项提供具有对 UIElements.dll 的引用的 Resgen.exe,以便其能够访问有关 PersonTable 结构的信息。You must use the /r option to supply Resgen.exe with a reference to UIElements.dll so that it can access information about the PersonTable structure. 如果使用 C#,请将 vbc 编译器名称替换为 csc,并将 .vb 扩展名替换为 .csIf you're using C#, replace the vbc compiler name with csc, and replace the .vb extension with .cs.

  
vbc /t:library UIElements.vb  
vbc CreateResources.vb /r:UIElements.dll  
CreateResources  
  
resgen UIResources.resx  /r:UIElements.dll  
vbc GetObject.vb /r:UIElements.dll /resource:UIResources.resources  
  
GetObject.exe  
  

注解

GetObject方法用于检索非字符串资源。The GetObject method is used to retrieve non-string resources. 其中包括属于基元数据类型Int32 (例如或Double)、 System.Drawing.Bitmap位图(如对象)或自定义序列化对象的值。These include values that belong to primitive data types such as Int32 or Double, bitmaps (such as a System.Drawing.Bitmap object), or custom serialized objects. 通常情况下,必须将返回的对象强制C#转换(在中)或转换(Visual Basic)为适当类型的对象。Typically, the returned object must be cast (in C#) or converted (in Visual Basic) to an object of the appropriate type.

返回的资源针对当前线程的 UI 区域性进行了本地化,后者由CultureInfo.CurrentUICulture属性定义。The returned resource is localized for the UI culture of the current thread, which is defined by the CultureInfo.CurrentUICulture property. 如果没有为此区域性本地化该资源,资源管理器将使用回退规则加载相应的资源。If the resource is not localized for that culture, the resource manager uses fallback rules to load an appropriate resource. 如果未找到可用的本地化资源集,则ResourceManager回退到默认区域性的资源。If no usable set of localized resources is found, the ResourceManager falls back on the default culture's resources. 如果找不到默认区域性的资源集,则该方法将引发MissingManifestResourceException异常,或者,如果资源集预期位于附属程序集中MissingSatelliteAssemblyException ,则会引发异常。If a resource set for the default culture is not found, the method throws a MissingManifestResourceException exception or, if the resource set is expected to reside in a satellite assembly, a MissingSatelliteAssemblyException exception. 如果资源管理器可以加载相应的资源集,但找不到名name为的资源, null则该方法将返回。If the resource manager can load an appropriate resource set but cannot find a resource named name, the method returns null.

属性确定name与资源名称的比较是否不区分大小写或是否区分大小写(默认值)。 IgnoreCaseThe IgnoreCase property determines whether the comparison of name with the names of resources is case-insensitive or case-sensitive (the default).

注意

此方法可能会引发比列出的更多异常。This method can throw more exceptions than are listed. 出现这种情况的原因之一是,此方法调用的方法会引发异常。One reason this might occur is if a method that this method calls throws an exception. 例如, FileLoadException如果在部署或安装附属程序集时出错,则可能会引发异常; 如果在反SerializationException序列化类型时用户定义的类型引发用户定义的异常,则可能引发异常。For example, a FileLoadException exception might be thrown if an error was made deploying or installing a satellite assembly, or a SerializationException exception might be thrown if a user-defined type throws a user-defined exception when the type is deserialized.

性能注意事项Performance Considerations

如果使用同一个GetObject name参数多次调用方法,则不依赖于每次调用返回对同一对象的引用的方法。If you call the GetObject method multiple times with the same name parameter, do not depend on the method returning a reference to the same object with each call. 这是因为GetObject方法可以返回对缓存中现有资源对象的引用,也可以重新加载资源并返回对新资源对象的引用。This is because the GetObject method can return a reference to an existing resource object in a cache, or it can reload the resource and return a reference to a new resource object.

安全性

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.

另请参阅

GetObject(String, CultureInfo)

获取为指定区域性本地化的指定非字符串资源的值。Gets the value of the specified non-string resource localized for the specified culture.

public:
 virtual System::Object ^ GetObject(System::String ^ name, System::Globalization::CultureInfo ^ culture);
public virtual object GetObject (string name, System.Globalization.CultureInfo culture);
abstract member GetObject : string * System.Globalization.CultureInfo -> obj
override this.GetObject : string * System.Globalization.CultureInfo -> obj
Public Overridable Function GetObject (name As String, culture As CultureInfo) As Object

参数

name
String

要获取的资源名。The name of the resource to get.

culture
CultureInfo

要针对其本地化资源的区域性。The culture for which the resource is localized. 如果资源未本地化为此区域性,则资源管理器使用回退规则找到适当的资源。If the resource is not localized for this culture, the resource manager uses fallback rules to locate an appropriate resource.

如果此值为 null,则 CultureInfo 对象使用 CurrentUICulture 属性来获取。If this value is null, the CultureInfo object is obtained by using the CurrentUICulture property.

返回

为指定区域性本地化的资源的值。The value of the resource, localized for the specified culture. 如果相应的资源集存在,但无法找到 name,该方法返回 nullIf an appropriate resource set exists but name cannot be found, the method returns null.

异常

name 参数为 nullThe name parameter is null.

未找到可用的资源集,并且没有默认区域性的资源。No usable set of resources have been found, and there are no default culture resources. 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

默认区域性的资源位于无法找到的附属程序集。The default culture's resources reside in a satellite assembly that could not be found. 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

示例

下面的示例使用 GetObject(String, CultureInfo) 方法反序列化一个自定义对象。The following example uses the GetObject(String, CultureInfo) method to deserialize a custom object. 该示例包含一个名为 NumberInfo.cs (NumberInfo)的源代码文件,如果使用 Visual Basic)来定义以下名为Numbers的结构。The example includes a source code file named NumberInfo.cs (NumberInfo.vb if you're using Visual Basic) that defines the following structure named Numbers. 此结构旨在供简单的教育应用程序使用,此应用程序讲授非英语的学生,使其以英语计算为10。This structure is intended to be used by a simple educational app that teaches non-English speaking students to count to ten in English. 请注意, Numbers类是SerializableAttribute用特性标记的。Note that the Numbers class is marked with the SerializableAttribute attribute.

using System;

[Serializable] public class Numbers
{
   public readonly string One;
   public readonly string Two;
   public readonly string Three;
   public readonly string Four;
   public readonly string Five;
   public readonly string Six;
   public readonly string Seven;
   public readonly string Eight;
   public readonly string Nine;
   public readonly string Ten;

   public Numbers(string one, string two, string three, string four, 
                  string five, string six, string seven, string eight,
                  string nine, string ten)
   {                     
      this.One = one;
      this.Two = two;
      this.Three = three;
      this.Four = four;
      this.Five = five;
      this.Six = six;
      this.Seven = seven;
      this.Eight = eight;
      this.Nine = nine;
      this.Ten = ten;                                    
   }                  
}
<Serializable> Public Class Numbers
   Public Readonly One As String
   Public ReadOnly Two As String
   Public ReadOnly Three As String
   Public ReadOnly Four As String
   Public ReadOnly Five As String
   Public ReadOnly Six As String
   Public ReadOnly Seven As String
   Public ReadOnly Eight As String
   Public ReadOnly Nine As String
   Public ReadOnly Ten As String
   
   Public Sub New(one As String, two As String, three As String, four As String, 
                  five As String, six As String, seven As String, eight As String,
                  nine As String, ten As String)
      Me.One = one
      Me.Two = two
      Me.Three = three
      Me.Four = four
      Me.Five = five
      Me.Six = six
      Me.Seven = seven
      Me.Eight = eight
      Me.Nine = nine
      Me.Ten = ten                                    
   End Sub                  
End Class

以下来自名为 CreateResources.cs 的文件(用于 Visual Basic 的位于)的源代码将为默认英语语言创建 XML 资源文件,以及法语、葡萄牙语和俄语。The following source code from a file named CreateResources.cs (CreateResources.vb for Visual Basic) creates XML resource files for the default English language, as well as for the French, Portuguese, and Russian languages.

using System;
using System.Resources;

public class CreateResource
{
   public static void Main()
   {
      Numbers en = new Numbers("one", "two", "three", "four", "five",
                               "six", "seven", "eight", "nine", "ten");
      CreateResourceFile(en, "en");
      Numbers fr = new Numbers("un", "deux", "trois", "quatre", "cinq", 
                               "six", "sept", "huit", "neuf", "dix");
      CreateResourceFile(fr, "fr");
      Numbers pt = new Numbers("um", "dois", "três", "quatro", "cinco", 
                               "seis", "sete", "oito", "nove", "dez");
      CreateResourceFile(pt, "pt"); 
      Numbers ru = new Numbers("один", "два", "три", "четыре", "пять", 
                               "шесть", "семь", "восемь", "девять", "десять");                                                       
      CreateResourceFile(ru, "ru");
   }

   public static void CreateResourceFile(Numbers n, string lang)
   {
      string filename = @".\NumberResources" + 
                        (lang != "en" ? "." + lang : "" ) +
                        ".resx";
      ResXResourceWriter rr = new ResXResourceWriter(filename);
      rr.AddResource("Numbers", n);
      rr.Generate();
      rr.Close();    
   }
}
Imports System.Resources

Module CreateResource
   Public Sub Main()
      Dim en As New Numbers("one", "two", "three", "four", "five",
                            "six", "seven", "eight", "nine", "ten")
      CreateResourceFile(en, "en")
      Dim fr As New Numbers("un", "deux", "trois", "quatre", "cinq", 
                            "six", "sept", "huit", "neuf", "dix")
      CreateResourceFile(fr, "fr")
      Dim pt As New Numbers("um", "dois", "três", "quatro", "cinco", 
                            "seis", "sete", "oito", "nove", "dez")
      CreateResourceFile(pt, "pt") 
      Dim ru As New Numbers("один", "два", "три", "четыре", "пять", 
                            "шесть", "семь", "восемь", "девять", "десять")                                                       
      CreateResourceFile(ru, "ru")
   End Sub

   Public Sub CreateResourceFile(n As Numbers, lang As String)
      Dim filename As String = ".\NumberResources" + 
                               If(lang <> "en", "." + lang, "") +
                               ".resx"
      Dim rr As New ResXResourceWriter(filename)
      rr.AddResource("Numbers", n)
      rr.Generate()
      rr.Close()    
   End Sub
End Module

以下应用使用资源,这会将当前 UI 区域性设置为法语(法国)、葡萄牙语(巴西)或俄语(俄罗斯)。The resources are consumed by the following app, which sets the current UI culture to French (France), Portuguese (Brazil), or Russian (Russia). 它调用GetObject(String)方法来Numbers获取GetObject(String, CultureInfo)包含本地化数字的对象,并调用方法来获取Numbers包含英文数字的对象。It calls the GetObject(String) method to get a Numbers object that contains localized numbers and the GetObject(String, CultureInfo) method to get a Numbers object that contains English language numbers. 然后,它使用当前 UI 区域性和英语显示奇数。It then displays odd numbers using the current UI culture and the English language. 源代码文件的名称为 ShowNumbers.cs (ShowNumbers)。The source code file is named ShowNumbers.cs (ShowNumbers.vb).

using System;
using System.Globalization;
using System.Resources; 
using System.Threading;

[assembly:NeutralResourcesLanguageAttribute("en-US")]

public class Example
{
   static string[] cultureNames = { "fr-FR", "pt-BR", "ru-RU" };
   
   public static void Main()
   {
      // Make any non-default culture the current culture.
      Random rnd = new Random();
      CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureNames[rnd.Next(0, cultureNames.Length)]);
      Thread.CurrentThread.CurrentUICulture = culture;
      Console.WriteLine("The current culture is {0}\n", CultureInfo.CurrentUICulture.Name);
      CultureInfo enCulture = CultureInfo.CreateSpecificCulture("en-US"); 

      ResourceManager rm = new ResourceManager(typeof(NumberResources));
      Numbers numbers = (Numbers) rm.GetObject("Numbers");
      Numbers numbersEn = (Numbers) rm.GetObject("Numbers", enCulture);
      Console.WriteLine("{0} --> {1}", numbers.One, numbersEn.One); 
      Console.WriteLine("{0} --> {1}", numbers.Three, numbersEn.Three); 
      Console.WriteLine("{0} --> {1}", numbers.Five, numbersEn.Five); 
      Console.WriteLine("{0} --> {1}", numbers.Seven, numbersEn.Seven); 
      Console.WriteLine("{0} --> {1}\n", numbers.Nine, numbersEn.Nine); 
   }
}

internal class NumberResources
{
}
// The example displays output like the following:
//       The current culture is pt-BR
//       
//       um --> one
//       três --> three
//       cinco --> five
//       sete --> seven
//       nove --> nine
Imports System.Globalization
Imports System.Resources 
Imports System.Threading

<assembly:NeutralResourcesLanguageAttribute("en-US")>

Module Example
   Dim cultureNames() As String = { "fr-FR", "pt-BR", "ru-RU" }
   
   Public Sub Main()
      ' Make any non-default culture the current culture.
      Dim rnd As New Random
      Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureNames(rnd.Next(0, cultureNames.Length)))
      Thread.CurrentThread.CurrentUICulture = culture
      Console.WriteLine("The current culture is {0}", CultureInfo.CurrentUICulture.Name)
      Console.WriteLine()
      Dim enCulture As CultureInfo = CultureInfo.CreateSpecificCulture("en-US") 

      Dim rm As New ResourceManager(GetType(NumberResources))
      Dim numbers As Numbers = CType(rm.GetObject("Numbers"), Numbers)
      Dim numbersEn As Numbers = CType(rm.GetObject("Numbers", enCulture), Numbers)
      Console.WriteLine("{0} --> {1}", numbers.One, numbersEn.One) 
      Console.WriteLine("{0} --> {1}", numbers.Three, numbersEn.Three) 
      Console.WriteLine("{0} --> {1}", numbers.Five, numbersEn.Five) 
      Console.WriteLine("{0} --> {1}", numbers.Seven, numbersEn.Seven) 
      Console.WriteLine("{0} --> {1}", numbers.Nine, numbersEn.Nine) 
      Console.WriteLine()       
   End Sub
End Module


Friend Class NumberResources
End Class

' The example displays output like the following:
'       The current culture is pt-BR
'       
'       um --> one
'       três --> three
'       cinco --> five
'       sete --> seven
'       nove --> nine

您可以使用以下批处理文件生成并执行该示例的 Visual Basic 版本。You can use the following batch file to build and execute the Visual Basic version of the example. C#如果使用的是,请vbccsc替换为,并.vb将扩展名.cs替换为。If you're using C#, replace vbc with csc, and replace the .vb extension with .cs.

  
vbc /t:library NumberInfo.vb  
  
vbc CreateResources.vb /r:NumberInfo.dll  
CreateResources  
  
resgen NumberResources.resx /r:NumberInfo.dll  
  
resgen NumberResources.fr.resx /r:Numberinfo.dll  
Md fr  
al /embed:NumberResources.fr.resources /culture:fr /t:lib /out:fr\ShowNumbers.resources.dll  
  
resgen NumberResources.pt.resx  /r:Numberinfo.dll  
Md pt  
al /embed:NumberResources.pt.resources /culture:pt /t:lib /out:pt\ShowNumbers.resources.dll  
  
resgen NumberResources.ru.resx /r:Numberinfo.dll  
Md ru  
al /embed:NumberResources.ru.resources /culture:ru /t:lib /out:ru\ShowNumbers.resources.dll  
  
vbc ShowNumbers.vb /r:NumberInfo.dll /resource:NumberResources.resources  
ShowNumbers.exe  

注解

GetObject(String, CultureInfo)方法用于检索非字符串资源。The GetObject(String, CultureInfo) method is used to retrieve non-string resources. 其中包括属于基元数据类型Int32 (例如或Double)、 System.Drawing.Bitmap位图(如对象)或自定义序列化对象的值。These include values that belong to primitive data types such as Int32 or Double, bitmaps (such as a System.Drawing.Bitmap object), or custom serialized objects. 通常情况下,必须将返回的对象强制C#转换(在中)或转换(Visual Basic)为适当类型的对象。Typically, the returned object must be cast (in C#) or converted (in Visual Basic) to an object of the appropriate type.

返回的资源culture针对指定的区域性进行了本地化; 如果culturenull,则为CultureInfo.CurrentUICulture属性所指定的区域性。The returned resource is localized for the culture that is specified by culture, or for the culture that is specified by the CultureInfo.CurrentUICulture property if culture is null. 如果没有为此区域性本地化该资源,资源管理器将使用回退规则加载相应的资源。If the resource is not localized for that culture, the resource manager uses fallback rules to load an appropriate resource. 如果未找到可用的本地化资源集,资源管理器将回退到默认区域性的资源。If no usable set of localized resources is found, the resource manager falls back on the default culture's resources. 如果找不到默认区域性的资源集,则该方法将引发MissingManifestResourceException异常,或者,如果资源集预期位于附属程序集中MissingSatelliteAssemblyException ,则会引发异常。If a resource set for the default culture is not found, the method throws a MissingManifestResourceException exception or, if the resource set is expected to reside in a satellite assembly, a MissingSatelliteAssemblyException exception. 如果资源管理器可以加载相应的资源集,但找不到名name为的资源, null则该方法将返回。If the resource manager can load an appropriate resource set but cannot find a resource named name, the method returns null.

属性确定name与资源名称的比较是否不区分大小写(默认值)或区分大小写。 IgnoreCaseThe IgnoreCase property determines whether the comparison of name with the names of resources is case-insensitive (the default) or case-sensitive.

注意

此方法可能会引发比列出的更多异常。This method can throw more exceptions than are listed. 出现这种情况的原因之一是,此方法调用的方法会引发异常。One reason this might occur is if a method that this method calls throws an exception. 例如, FileLoadException如果在部署或安装附属程序集时出错,则可能会引发异常; 如果在反SerializationException序列化类型时用户定义的类型引发用户定义的异常,则可能引发异常。For example, a FileLoadException exception might be thrown if an error was made deploying or installing a satellite assembly, or a SerializationException exception might be thrown if a user-defined type throws a user-defined exception when the type is deserialized.

性能注意事项Performance Considerations

如果使用同一个GetObject name参数多次调用方法,则不依赖于每次调用返回对同一对象的引用的方法。If you call the GetObject method multiple times with the same name parameter, do not depend on the method returning a reference to the same object with each call. 这是因为GetObject方法可以返回对缓存中现有资源对象的引用,也可以重新加载资源并返回对新资源对象的引用。This is because the GetObject method can return a reference to an existing resource object in a cache, or it can reload the resource and return a reference to a new resource object.

安全性

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.

线程安全性

方法GetObject(String, CultureInfo)是线程安全的。The GetObject(String, CultureInfo) method is thread safe.

另请参阅

适用于