[Serializable] |
Using the methods of ResourceManager, a caller can access the resources for a particular culture using the ResourceManager.GetObject and ResourceManager.GetString methods. By default, these methods return the resource for the culture determined by the current cultural settings of the thread that made the call. (See Thread.CurrentUICulture for more information.) A caller can use the ResourceManager.GetResourceSet method to obtain a ResourceSet, which represents the resources for a particular culture, ignoring culture fallback rules. You can then use the ResourceSet to access the resources, localized for that culture, by name.
Ideally, you should create resources for every language, or at least a meaningful subset of the language. The resource file names follow the naming convention basename.cultureName.resources, where basename is the name of the application or the name of a class, depending on the granularity you want. The CultureInfo 's CultureInfo.Name property is used to determine the cultureName. A resource for the neutral culture (returned by CultureInfo.InvariantCulture) should be named basename.resources.
For example, suppose that an assembly has several resources in a resource file with the basename
"MyResources"
. These resource files will have names such as
"MyResources.ja-JP.resources"
,
"MyResources.de.resources"
,
"MyResources.zh-CHS.resources"
, or
"MyResources.fr-BE.resources"
, which contain resources respectively for Japanese, German, simplified Chinese, and French (Belgium). The default resource file should be named MyResources.resources. The culture-specific resource files are commonly packaged in satellite assemblies for each culture. The default resource file should be in your main assembly.
Now suppose that a ResourceManager has been created to represent the resources with this basename. Using the ResourceManager, you can obtain a ResourceSet that encapsulates
"MyResources.ja-JP.resources"
by calling
GetResourceSet(new CultureInfo ("ja-JP"), TRUE, FALSE)
. Or, if you know that
"MyResources"
contains a resource named
"TOOLBAR_ICON"
, you can obtain the value of that resource localized for Japan by calling
GetObject("TOOLBAR_ICON", new CultureInfo("ja-JP"))
.
While not strictly necessary for the most basic uses of the ResourceManager, publically shipping assemblies should use the SatelliteContractVersionAttribute to support versioning your main assembly without redeploying your satellites, and the NeutralResourcesLanguageAttribute to avoid looking up a satellite assembly that might never exist. For assistance with localization of Windows Forms dialogs, see the tool in the Microsoft .NET Framework SDK. To learn more about creating satellite assemblies, see the conceptual topic at MSDN: creatingsatelliteassemblies.
To learn more about setting up and creating resources, see the conceptual topic at MSDN: creatingusingresources.
Using standalone .resources files in an ASP.NET application will break XCOPY deployment, because the resources remain locked until they are explicitly released by the ResourceManager.ReleaseAllResources method. If you want to deploy resources into ASP.NET applications, you should compile your .resources into satellite assemblies. Resources marked as private are accesible only in the assembly in which they are placed. Because a satellite assembly contains no code, resources private to it become unavailable thrugh any mechanism. Therefore, resources in satellite assemblies should always be public so that they are accessible from your main assembly. Resources embedded in your main assembly are accessible to your main assembly, whether private or public.ctor #2 | Overloaded:.ctor(Type resourceSource) Creates a ResourceManager that looks up resources in satellite assemblies based on information from the specified Type. |
ctor #3 | Overloaded:.ctor(string baseName, Assembly assembly) 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. |
ctor #4 | Overloaded:.ctor(string baseName, Assembly assembly, Type usingResourceSet) 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. |
HeaderVersionNumber | A constant readonly value indicating the version of resource file headers that the current implementation of ResourceManager can interpret and produce. |
MagicNumber | Holds the number used to identify resource files. |
BaseName | Read-only Gets the root name of the resource files that the ResourceManager searches for resources. |
IgnoreCase | Read-write Gets or sets a Boolean value indicating whether the current instance of ResourceManager allows case-insensitive resource lookups in the ResourceManager.GetString and ResourceManager.GetObject methods. |
ResourceSetType | Read-only Gets the Type of the ResourceSet the ResourceManager uses to construct a ResourceSet object. |
CreateFileBasedResourceManager | Returns a ResourceManager that searches a specific directory for resources instead of in the assembly manifest. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetObject | Overloaded:GetObject(string name) Returns the value of the specified Object resource. |
GetObject | Overloaded:GetObject(string name, CultureInfo culture) Gets the value of the Object resource localized for the specified culture. |
GetResourceSet | Gets the ResourceSet for a particular culture. |
GetString | Overloaded:GetString(string name) Returns the value of the specified String resource. |
GetString | Overloaded:GetString(string name, CultureInfo culture) Gets the value of the String resource localized for the specified culture. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
ReleaseAllResources | Tells the ResourceManager to call ResourceSet.Close on all ResourceSet objects and release all resources. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the ResourceManager class with default values. |
BaseNameField | Indicates the root name of the resource files that the ResourceManager searches for resources. |
MainAssembly | Indicates the main Assembly that contains the resources. |
ResourceSets | Contains a Hashtable that returns a mapping from cultures to ResourceSet objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
GetNeutralResourcesLanguage | Returns the CultureInfo for the main assembly's neutral resources by reading the value of the NeutralResourcesLanguageAttribute on a specified Assembly. |
GetResourceFileName | Generates the name for the resource file for the given CultureInfo. |
GetSatelliteContractVersion | Returns the Version specified by the SatelliteContractVersionAttribute in the given assembly. |
InternalGetResourceSet | Provides the implementation for finding a ResourceSet. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
protected ResourceManager(); |
public ResourceManager( |
resourceSource
Exception Type | Condition |
---|---|
ArgumentNullException | The resourceSource parameter is null. |
baseName
assembly
Exception Type | Condition |
---|---|
ArgumentNullException | The baseName or assembly parameter is null. |
This constructor uses the system provided ResourceSet implementation. To use a custom resource file format, you should derive from the ResourceSet class, override ResourceSet.GetDefaultReader and ResourceSet.GetDefaultWriter, and pass that type to the constructor that takes a Type as the third parameter. Using a custom ResourceSet can be useful for controlling resource caching policy or supporting your own resource file format, but is generally not necessary.
baseName
assembly
usingResourceSet
Exception Type | Condition |
---|---|
ArgumentException | usingResourceset is not a derived class of ResourceSet. |
ArgumentNullException | The baseName or assembly parameter is null. |
You can specify a ResourceSet implementation to be used. If you do not need a specific ResourceSet implementation but would like to use a custom resource file format, you should derive from the ResourceSet class, override ResourceSet.GetDefaultReader and ResourceSet.GetDefaultWriter, and pass that type to this constructor.
protected string BaseNameField;
|
public static readonly int HeaderVersionNumber;
|
public static readonly int MagicNumber;
|
If the ResourceManager.MagicNumber is found, the bytes following it will be a version number for a ResourceManager header, followed by a number indicating how many bytes should be skipped to get past this header. The next number indicates the version of the ResourceManager that created the header, followed by version specific information.
The version number for the current implementation is one. The next bytes are a length-prefixed string containing the name of an IResourceReader, which can read this file.
protected Assembly MainAssembly;
|
protected Hashtable ResourceSets;
|
public virtual string BaseName {get;}
|
public virtual bool IgnoreCase {get; set;}
|
public virtual Type ResourceSetType {get;}
|
public static ResourceManager CreateFileBasedResourceManager( |
baseName
resourceDir
usingResourceSet
Exception Type | Condition |
---|---|
ArgumentException | The baseName ends in .resources. |
ArgumentNullException | The baseName parameter is null. |
You can specify a ResourceSet implementation. If you do not need a specific ResourceSet implementation, but would like to use a custom resource file format, you should derive from the ResourceSet class, override ResourceSet.GetDefaultReader and ResourceSet.GetDefaultWriter, and pass that type to this constructor.
Using standalone .resources files in an ASP.NET application will break XCOPY deployment because the resources remain locked until they are explicitly released by the ResourceManager.ReleaseAllResources method. If you want to deploy resources into ASP.NET applications, compile your .resources into satellite assemblies.
~ResourceManager(); |
public virtual int GetHashCode(); |
protected static CultureInfo GetNeutralResourcesLanguage( |
a
name
Exception Type | Condition |
---|---|
ArgumentNullException | The name parameter is null. |
MissingManifestResourceException | No usable set of resources has been found, and there are no neutral culture resources. |
If no usable set of resources has been found, the ResourceManager falls back on the neutral culture's resources, which are expected to be included in the main assembly. If an appropriate culture resource has not been found, a MissingManifestResourceException is thrown.
public virtual object GetObject( |
name
culture
If this value is null, the CultureInfo is obtained using the culture's CultureInfo.CurrentUICulture property.
The CultureInfo object that represents the culture for which the resource is localized. Note that if the resource is not localized for this culture, the lookup will fall back using the culture's CultureInfo.Parent property, stopping after checking in the neutral culture.If this value is null, the CultureInfo is obtained using the culture's CultureInfo.CurrentUICulture property.
Exception Type | Condition |
---|---|
ArgumentNullException | The name parameter is null. |
MissingManifestResourceException | No usable set of resources have been found, and there are no neutral culture resources. |
If no usable set of resources has been found, the ResourceManager falls back on the neutral culture's resources, which are expected to be included in the main assembly. If an appropriate culture resource has not been found, a MissingManifestResourceException is thrown.
protected virtual string GetResourceFileName( |
culture
A derived class can override this method to look for a different extension, such as ".ResX", or a completely different scheme for naming files.
public virtual ResourceSet GetResourceSet( |
culture
createIfNotExists
tryParents
Exception Type | Condition |
---|---|
ArgumentNullException | The culture parameter is null. |
The parameters let you control whether the ResourceSet is created if it hasn't been loaded yet, and whether parent CultureInfo objects should be loaded as well for resource inheritance.
a
Exception Type | Condition |
---|---|
ArgumentException | The Version found in the assembly a is invalid. |
name
Exception Type | Condition |
---|---|
ArgumentNullException | The name parameter is null. |
InvalidOperationException | The value of the specified resource is not a string. |
MissingManifestResourceException | No usable set of resources has been found, and there are no neutral culture resources. |
If no usable set of resources has been found, the ResourceManager falls back on the neutral culture's resources, which are expected to be in the main assembly. If an appropriate culture resource has not been found, a MissingManifestResourceException is thrown.
public virtual string GetString( |
name
culture
If this value is null, the CultureInfo is obtained using the culture's CultureInfo.CurrentUICulture property.
The CultureInfo object that represents the culture for which the resource is localized. Note that if the resource is not localized for this culture, the lookup will fall back using the culture's CultureInfo.Parent property, stopping after looking in the neutral culture.If this value is null, the CultureInfo is obtained using the culture's CultureInfo.CurrentUICulture property.
Exception Type | Condition |
---|---|
ArgumentNullException | The name parameter is null. |
InvalidOperationException | The value of the specified resource is not a String. |
MissingManifestResourceException | No usable set of resources has been found, and there are no neutral culture resources. |
If no usable set of resources has been found, the ResourceManager falls back on the neutral culture's resources, which are expected to be in the main assembly. If an appropriate culture resource has not been found, a MissingManifestResourceException is thrown.
public Type GetType(); |
protected virtual ResourceSet InternalGetResourceSet( |
culture
createIfNotExists
tryParents
Exception Type | Condition |
---|---|
MissingManifestResourceException | The main assembly does not contain a .resources file and it is required to look up a resource. |
protected object MemberwiseClone(); |
public virtual void ReleaseAllResources(); |
This can be useful in some complex threading scenarios, where creating a new ResourceManager is the appropriate behavior.
This method can also be used in situations where the .resources files opened by the current ResourceManager have to be released deterministically, without waiting for the ResourceManager to go completely out of scope and be garbage collected.
public virtual string ToString(); |