Export (0) Print
Expand All

ResourceManager.GetObject Method (String, CultureInfo)

Gets the value of the specified non-string resource localized for the specified culture.

Namespace:  System.Resources
Assembly:  mscorlib (in mscorlib.dll)

public virtual Object GetObject(
	string name,
	CultureInfo culture
)

Parameters

name
Type: System.String
The name of the resource to get.
culture
Type: System.Globalization.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.
If this value is null, the CultureInfo object is obtained by using the culture's CultureInfo.CurrentUICulture property.

Return Value

Type: System.Object
The value of the resource, localized for the specified culture. If an appropriate resource set exists but name cannot be found, the method returns null.

ExceptionCondition
ArgumentNullException

The name parameter is null.

MissingManifestResourceException

No usable set of resources have been found, and there are no neutral culture resources. For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

MissingSatelliteAssemblyException

The default culture's resources reside in a satellite assembly that could not be found. For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

The GetObject method is used to retrieve non-string resources. 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. Typically, the returned object must be cast (in C#) or converted (in Visual Basic) to an object of the appropriate type.

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 has not been 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. If a resource set for the default culture is not found, the method throws a MissingManifestResourceException exception. If the resource manager can load an appropriate resource set but cannot find a resource named name, the method returns null.

The IgnoreCase property determines whether the comparison of name with the names of resources is case-insensitive (the default) or case-sensitive.

Caution noteCaution

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. For example, a FileLoadException might be thrown if an error was made deploying or installing a satellite assembly, or a SerializationException might be thrown if a user-defined type throws a user-defined exception when the type is deserialized.

Performance Considerations

If you call the GetObject method multiple times with the same name parameter, do not depend on the return value being a reference to the same object. This is because the GetObject method can return a reference to an existing resource object in a cache, or can reload the resource and return a reference to a new resource object.

The following example uses the GetObject method to deserialize a custom object. 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. This structure is intended to be used by a simple educational app that teaches non-English speaking students to count to ten in English. 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;                                    
   }                  
}


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();    
   }
}


The resources are consumed by the following app, which sets the current UI culture to French (France), Portuguese (Brazil), or Russian (Russia). 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. It then dnotdirisplays odd numbers using the current UI culture and the English language. 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


You can use the following batch file to build and execute the Visual Basic version of the example. 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

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft