ResourceManager Constructors
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Initializes a new instance of the ResourceManager class.
Overloads
ResourceManager() |
Initializes a new instance of the ResourceManager class with default values. |
ResourceManager(Type) |
Initializes a new instance of the ResourceManager class that looks up resources in satellite assemblies based on information from the specified type object. |
ResourceManager(String, Assembly) |
Initializes a new instance of the ResourceManager class that looks up resources contained in files with the specified root name in the given assembly. |
ResourceManager(String, Assembly, Type) |
Initializes a new instance of the ResourceManager class that uses a specified ResourceSet class to look up resources contained in files with the specified root name in the given assembly. |
ResourceManager()
Initializes a new instance of the ResourceManager class with default values.
protected:
ResourceManager();
protected ResourceManager ();
Protected Sub New ()
Remarks
This constructor is useful only if you write your own class that derives from the ResourceManager class.
Applies to
ResourceManager(Type)
Initializes a new instance of the ResourceManager class that looks up resources in satellite assemblies based on information from the specified type object.
public:
ResourceManager(Type ^ resourceSource);
public ResourceManager (Type resourceSource);
new System.Resources.ResourceManager : Type -> System.Resources.ResourceManager
Public Sub New (resourceSource As Type)
Parameters
- resourceSource
- Type
A type from which the resource manager derives all information for finding .resources files.
Exceptions
The resourceSource
parameter is null
.
Examples
The following example uses the ResourceManager(Type) constructor to instantiate a ResourceManager object. It consists of resources compiled from .txt files for the English (en), French (France) (fr-FR), and Russian (Russia) (ru-RU) cultures. The example changes the current culture and current UI culture to English (United States), French (France), Russian (Russia), and Swedish (Sweden). It then calls the GetString(String) method to retrieve the localized string, which displays a greeting that depends on the time of day.
The example requires three text-based resource files, as listed in the following table. Each file includes string resources named Morning
, Afternoon
, and Evening
.
Culture | File name | Resource name | Resource value |
---|---|---|---|
en-US | GreetingResources.txt | Morning |
Good morning |
en-US | GreetingResources.txt | Afternoon |
Good afternoon |
en-US | GreetingResources.txt | Evening |
Good evening |
fr-FR | GreetingResources.fr-FR.txt | Morning |
Bonjour |
fr-FR | GreetingResources.fr-FR.txt | Afternoon |
Bonjour |
fr-FR | GreetingResources.fr-FR.txt | Evening |
Bonsoir |
ru-RU | GreetingResources.ru-RU.txt | Morning |
Доброе утро |
ru-RU | GreetingResources.ru-RU.txt | Afternoon |
Добрый день |
ru-RU | GreetingResources.ru-RU.txt | Evening |
Добрый вечер |
You can use the following batch file to compile the Visual Basic example and create an executable named Greet.exe. To compile with C#, change the compiler name from vbc
to csc
and the file extension from .vb
to .cs
.
resgen GreetingResources.txt
vbc Greet.vb /resource: GreetingResources.resources
md fr-FR
resgen GreetingResources.fr-FR.txt
al /out:fr-FR\Greet.resources.dll /culture:fr-FR /embed: GreetingResources.fr-FR.resources
md ru-RU
resgen GreetingResources.ru-RU.txt
al /out:ru-RU\Greet.resources.dll /culture:ru-RU /embed: GreetingResources.ru-RU.resources
Here's the source code for the example (ShowDate.vb for the Visual Basic version or ShowDate.cs for the C# version of the code).
using System;
using System.Resources;
using System.Globalization;
using System.Threading;
[assembly:NeutralResourcesLanguage("en")]
public class Example
{
public static void Main()
{
string[] cultureNames = {"en-US", "fr-FR", "ru-RU", "sv-SE" };
DateTime noon = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
DateTime.Now.Day, 12,0,0);
DateTime evening = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
DateTime.Now.Day, 18, 0, 0);
ResourceManager rm = new ResourceManager(typeof(GreetingResources));
foreach (var cultureName in cultureNames) {
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureName);
Console.WriteLine("The current UI culture is {0}",
CultureInfo.CurrentUICulture.Name);
if (DateTime.Now < noon)
Console.WriteLine("{0}!", rm.GetString("Morning"));
else if (DateTime.Now < evening)
Console.WriteLine("{0}!", rm.GetString("Afternoon"));
else
Console.WriteLine("{0}!", rm.GetString("Evening"));
Console.WriteLine();
}
}
internal class GreetingResources
{
}
}
// The example displays output like the following:
// The current UI culture is en-US
// Good afternoon!
//
// The current UI culture is fr-FR
// Bonjour!
//
// The current UI culture is ru-RU
// Добрый день!
//
// The current UI culture is sv-SE
// Good afternoon!
Imports System.Resources
Imports System.Globalization
Imports System.Threading
<Assembly:NeutralResourcesLanguage("en")>
Module Example
Public Sub Main()
Dim cultureNames() As String = {"en-US", "fr-FR", "ru-RU", "sv-SE" }
Dim noon As New Date(Date.Now.Year, Date.Now.Month,
Date.Now.Day, 12,0,0)
Dim evening As New Date(Date.Now.Year, Date.Now.Month,
Date.Now.Day, 18, 0, 0)
Dim rm As New ResourceManager(GetType(GreetingResources))
For Each cultureName In cultureNames
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureName)
Console.WriteLine("The current UI culture is {0}",
CultureInfo.CurrentUICulture.Name)
If Date.Now < noon Then
Console.WriteLine("{0}!", rm.GetString("Morning"))
ElseIf Date.Now < evening Then
Console.WriteLine("{0}!", rm.GetString("Afternoon"))
Else
Console.WriteLine("{0}!", rm.GetString("Evening"))
End If
Console.WriteLine()
Next
End Sub
End Module
Friend Class GreetingResources
End Class
' The example displays output like the following:
' The current UI culture is en-US
' Good afternoon!
'
' The current UI culture is fr-FR
' Bonjour!
'
' The current UI culture is ru-RU
' Добрый день!
'
' The current UI culture is sv-SE
' Good afternoon!
In addition to defining an app class named Example
, the source code defines an internal class whose name, GreetingResources
, is the same as the base name of the resource files. This makes it possible to successfully instantiate a ResourceManager object by calling the ResourceManager(Type) constructor.
Notice that the output displays the appropriate localized string except when the current UI culture is Swedish (Sweden), in which case it uses English language resources. Because Swedish language resources are unavailable, the app uses the resources of the default culture, as defined by the NeutralResourcesLanguageAttribute attribute, instead.
Remarks
Desktop Apps
In desktop apps, the resource manager uses the resourceSource
parameter to load a particular resource file as follows:
If the NeutralResourcesLanguageAttribute attribute is not used to indicate that the resources of the default culture reside in a satellite assembly, the resource manager assumes that the resource file for the default culture is found in the same assembly as the type specified by the
resourceSource
parameter.The resource manager assumes that the default resource file has the same base name as the type specified by the
resourceSource
parameter.The resource manager uses the default ResourceSet class to manipulate the resource file.
For example, given a type named MyCompany.MyProduct.MyType, the resource manager looks for a .resources file named MyCompany.MyProduct.MyType.resources in the assembly that defines MyType.
In Visual Studio, the Resource Designer automatically generates code that defines an internal
(in C#) or Friend
(in Visual Basic) class whose name is the same as the base name of the .resources file for the default culture. This makes it possible to instantiate a ResourceManager object and couple it with a particular set of resources by getting a type object whose name corresponds to the name of the resource, because as long as the class is visible to the compiler, the resources must be as well. For example, if a .resources file is named Resource1, the following statement instantiates a ResourceManager object to manage the .resources file named Resource1:
ResourceManager rm = new ResourceManager(typeof(Resource1));
If you're not using Visual Studio, you can create a class with no members whose namespace and name are the same as that of the default .resources file. The example provides an illustration.
Windows 8.x Apps
Important
Although the ResourceManager class is supported in Windows 8.x apps, we do not recommend its use. Use this class only when you develop Portable Class Library projects that can be used with Windows 8.x apps. To retrieve resources from Windows 8.x apps, use the Windows.ApplicationModel.Resources.ResourceLoader class instead.
In Windows 8.x apps, ResourceManager uses the resourceSource
parameter to infer the assembly, base name, and the namespace where the resource items can be located within the app's package resource index (PRI) file. For example, given a type named MyCompany.MyProduct.MyType that is defined in MyAssembly
, the resource manager looks for a resource set identifier named MyAssembly and looks for a scope MyCompany.MyProduct.MyType within that resource set. The resource manager searches for resource items under the default context (current culture, current high contrast setting, and so on) within this scope.
Applies to
ResourceManager(String, Assembly)
Initializes a new instance of the ResourceManager class that looks up resources contained in files with the specified root name in the given assembly.
public:
ResourceManager(System::String ^ baseName, System::Reflection::Assembly ^ assembly);
public ResourceManager (string baseName, System.Reflection.Assembly assembly);
new System.Resources.ResourceManager : string * System.Reflection.Assembly -> System.Resources.ResourceManager
Public Sub New (baseName As String, assembly As Assembly)
Parameters
- baseName
- String
The root name of the resource file without its extension but including any fully qualified namespace name. For example, the root name for the resource file named MyApplication.MyResource.en-US.resources is MyApplication.MyResource.
- assembly
- Assembly
The main assembly for the resources.
Exceptions
The baseName
or assembly
parameter is null
.
Examples
The following example uses a simple non-localized "Hello World" app to illustrate the ResourceManager(String, Assembly) constructor. The following shows the contents of a text file named ExampleResources.txt. When the app is compiled, the resource is embedded in the main app assembly.
Greeting=Hello
The text file can be converted to a binary resource file by using the Resource File Generator (ResGen.exe) at the command prompt as follows:
resgen ExampleResources.txt
The following example provides the executable code that instantiates a ResourceManager object, prompts the user to enter a name, and displays a greeting.
using System;
using System.Reflection;
using System.Resources;
public class Example
{
public static void Main()
{
// Retrieve the resource.
ResourceManager rm = new ResourceManager("ExampleResources" ,
typeof(Example).Assembly);
string greeting = rm.GetString("Greeting");
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("{0} {1}!", greeting, name);
}
}
// The example produces output similar to the following:
// Enter your name: John
// Hello John!
Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Module Example
Public Sub Main()
' Retrieve the resource.
Dim rm As New ResourceManager("ExampleResources",
GetType(Example).Assembly)
Dim greeting As String = rm.GetString("Greeting")
Console.Write("Enter your name: ")
Dim name As String = Console.ReadLine()
Console.WriteLine("{0} {1}!", greeting, name)
End Sub
End Module
' The example produces output similar to the following:
' Enter your name: John
' Hello John!
It can be compiled by using the following command in Visual Basic:
vbc Example.vb /resource:ExampleResources.resources
or by using the following command in C#:
csc Example.cs /resource:ExampleResources.resources
Note that the example retrieves a reference to the assembly that contains the resource file by passing a type defined in that assembly to the typeof
function (in C#) or the GetType
function (in Visual Basic) and retrieving the value of its Type.Assembly property.
Remarks
Desktop Apps
In desktop apps, the individual culture-specific resource files should be contained in satellite assemblies, and the default culture's resource file should be contained in the main assembly. A satellite assembly is assumed to contain resources for a single culture specified in that assembly's manifest, and is loaded as necessary.
Note
To retrieve resources from .resources files directly instead of retrieving them from assemblies, you must call the CreateFileBasedResourceManager method instead to instantiate a ResourceManager object.
If the resource file identified by baseName
cannot be found in assembly
, the method instantiates a ResourceManager object, but the attempt to retrieve a specific resource throws an exception, typically MissingManifestResourceException. For information about diagnosing the cause of the exception, see the "Handling the MissingManifestResourceException Exception" section of the ResourceManager class topic.
Windows 8.x Apps
Important
Although the ResourceManager class is supported in Windows 8.x apps, we do not recommend its use. Use this class only when you develop Portable Class Library projects that can be used with Windows 8.x apps. To retrieve resources from Windows 8.x apps, use the Windows.ApplicationModel.Resources.ResourceLoader class instead.
In Windows 8.x apps, the resource manager uses the simple name of the assembly
parameter to look up a matching resource set in the app's package resource index (PRI) file. The baseName
parameter is used to look up a resource item within the resource set. For example, the root name for PortableLibrary1.Resource1.de-DE.resources is PortableLibrary1.Resource1.
Notes to Inheritors
This constructor uses the system-provided ResourceSet implementation. To use a custom resource file format, you should derive from the ResourceSet class, override the GetDefaultReader() and GetDefaultWriter() methods, and pass that type to the ResourceManager(String, Assembly, Type) constructor. Using a custom ResourceSet can be useful for controlling resource caching policy or supporting your own resource file format, but is generally not necessary.
Applies to
ResourceManager(String, Assembly, Type)
Initializes a new instance of the ResourceManager class that uses a specified ResourceSet class to look up resources contained in files with the specified root name in the given assembly.
public:
ResourceManager(System::String ^ baseName, System::Reflection::Assembly ^ assembly, Type ^ usingResourceSet);
public ResourceManager (string baseName, System.Reflection.Assembly assembly, Type? usingResourceSet);
public ResourceManager (string baseName, System.Reflection.Assembly assembly, Type usingResourceSet);
new System.Resources.ResourceManager : string * System.Reflection.Assembly * Type -> System.Resources.ResourceManager
Public Sub New (baseName As String, assembly As Assembly, usingResourceSet As Type)
Parameters
- baseName
- String
The root name of the resource file without its extension but including any fully qualified namespace name. For example, the root name for the resource file named MyApplication.MyResource.en-US.resources is MyApplication.MyResource.
- assembly
- Assembly
The main assembly for the resources.
- usingResourceSet
- Type
The type of the custom ResourceSet to use. If null
, the default runtime ResourceSet object is used.
Exceptions
usingResourceset
is not a derived class of ResourceSet.
The baseName
or assembly
parameter is null
.
Remarks
The individual culture-specific resource files should be contained in satellite assemblies, and the default culture's resource file should be contained in the main assembly. A satellite assembly is assumed to contain resources for a single culture specified in that assembly's manifest, and is loaded as necessary.
Note
To retrieve resources from .resources files directly instead of retrieving them from assemblies, you must call the CreateFileBasedResourceManager method instead to instantiate a ResourceManager object.
If the resource file identified by baseName
cannot be found in assembly
, the method instantiates a ResourceManager object, but the attempt to retrieve a specific resource throws an exception, typically MissingManifestResourceException. For information about diagnosing the cause of the exception, see the "Handling the MissingManifestResourceException Exception" section of the ResourceManager class topic.
Note
The usingResourceSet
parameter is used to support your own resource format, and will commonly be null
. This is different from the constructor that takes a Type only.
Notes to Callers
This constructor lets you specify a ResourceSet implementation. If you do not want a specific ResourceSet implementation but would like to use a custom resource file format, you should derive from the ResourceSet class, override the GetDefaultReader() and GetDefaultWriter() methods, and pass that type to this constructor.
Applies to
Feedback
Submit and view feedback for