Export (0) Print
Expand All

ResourceManager Constructor (Type)

Initializes a new instance of the ResourceManager class that looks up resources in satellite assemblies based on information from the specified type object.

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

public:
ResourceManager(
	Type^ resourceSource
)

Parameters

resourceSource
Type: System::Type

A type from which the resource manager derives all information for finding .resources files.

ExceptionCondition
ArgumentNullException

The resourceSource parameter is nullptr.

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 Store Apps

Important noteImportant

Although the ResourceManager class is supported in Windows Store apps, we do not recommend its use. Use this class only when you develop Portable Class Library projects that can be used with Windows Store apps. To retrieve resources from Windows Store apps, use the Windows.ApplicationModel.Resources.ResourceLoader class instead.

In Windows Store 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.

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).

No code example is currently available or this language may not be supported.

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.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft