Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

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)
'Declaration
Public Sub New ( _
	resourceSource As Type _
)

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

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:


Dim rm As New ResourceManager(GetType(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.

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


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.

.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

Portable Class Library

Supported in: Portable Class Library

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.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.