Compartir a través de


ResourceManager Constructor (String, Assembly)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: October 2010

Initializes a new instance of the ResourceManager class that looks up resources contained in files derived from the specified root name using the given Assembly.

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

Syntax

'Declaration
Public Sub New ( _
    baseName As String, _
    assembly As Assembly _
)
public ResourceManager(
    string baseName,
    Assembly assembly
)

Parameters

  • baseName
    Type: System.String
    The root name of the resource file without its extension but including a fully qualified namespace name. For example, the root name for the resource file named "MyApplication.MyResource.en-US.resources" is "MyApplication.MyResource".

Exceptions

Exception Condition
ArgumentNullException

The baseName or assembly parameter is nulla null reference (Nothing in Visual Basic).

ArgumentException

assembly is not a run-time object.

Remarks

The individual resource files should be contained in satellite assemblies with the invariant culture's.resource file contained in the main assembly. A satellite assembly is assumed to contain resources for a single culture specified in that assembly's manifest, and are loaded as necessary.

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 a MissingManifestResourceException.

Notes to Implementers

This constructor uses the system-provided ResourceSet implementation. To use a custom resource file format, you should derive from the ResourceSet class, override GetDefaultReader and GetDefaultWriter, and pass that type to the ResourceManager 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.

Examples

The following example defines a new instance of the ResourceManager class that looks for executing assemblies under the item's root name. The code then checks for the culture of the currently executing thread and displays a culture-appropriate welcome message.

Imports System.Globalization
Imports System.Threading
Imports System.Resources
Imports System.Reflection

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Create a resource manager to retrieve resources.
      Dim rm As New ResourceManager("items", _
         [Assembly].GetExecutingAssembly())

      ' Get the culture of the currently executing thread.
      ' The value of ci will determine the culture of
      ' the resources that the resource manager retrieves.
      Dim ci As CultureInfo = Thread.CurrentThread.CurrentCulture

      ' Retrieve the value of the string resource named 
      ' "welcome" localized for the culture specified by ci.
      Dim str As [String] = rm.GetString("welcome", ci)
      outputBlock.Text &= str & vbCrLf
   End Sub
End Class
using System;
using System.Globalization;
using System.Threading;
using System.Resources;
using System.Reflection;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create a resource manager to retrieve resources.
      ResourceManager rm = new ResourceManager("items",
         Assembly.GetExecutingAssembly());

      // Get the culture of the currently executing thread.
      // The value of ci will determine the culture of
      // the resources that the resource manager retrieves.
      CultureInfo ci = Thread.CurrentThread.CurrentCulture;

      // Retrieve the value of the string resource named 
      // "welcome", localized for the culture specified by ci.
      String str = rm.GetString("welcome", ci);
      outputBlock.Text += str + "\n";
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

October 2010

Expanded the baseName parameter description and the Remarks section.

Customer feedback.