Tip
To see when to use the ResXResourceReader, ResXResourceWriter, ResXResourceSet, ResXResourceManager, HybridResourceManager and DynamicResourceManager
classes see the documentation of the KGySoft.Resources namespace.
[SerializableAttribute]
public class HybridResourceManager : ResourceManager,
IExpandoResourceManager, IDisposable
<SerializableAttribute>
Public Class HybridResourceManager
Inherits ResourceManager
Implements IExpandoResourceManager, IDisposable
[SerializableAttribute]
public ref class HybridResourceManager : public ResourceManager,
IExpandoResourceManager, IDisposable
[<SerializableAttribute>]
type HybridResourceManager =
class
inherit ResourceManager
interface IExpandoResourceManager
interface IDisposable
end
HybridResourceManager class is derived from ResourceManager and uses a ResXResourceManager internally. The HybridResourceManager combines the functionality of the regular ResourceManager and the ResXResourceManager classes. The source of the resources can be chosen by the Source property (see ResourceManagerSources enumeration). Enabling both binary and .resx resources makes possible to expand or override the resources originally come from binary resources. Just like the ResXResourceManager it is an IExpandoResourceManager implementation. The replacement and newly added content can be saved into .resx files.
See the Comparison with ResourceManager section to see all of the differences.
You can create compiled resources by Visual Studio and you can dynamically expand them by HybridResourceManager. The new and overridden content will be saved as .resx files. See the following example for a step-by-step guide.
using System;
using System.Globalization;
using KGySoft.Resources;
public class Program
{
public static void Main()
{
var enUS = CultureInfo.GetCultureInfo("en-US");
var en = enUS.Parent;
var inv = en.Parent; // same as CultureInfo.InvariantCulture
var resourceManager = new HybridResourceManager(
baseName: "ConsoleApp1.Resources.Resource1", // Or "MyResources" if you followed the tip above.
assembly: typeof(Program).Assembly, // This is the assembly contains the compiled resources
explicitResXBaseFileName: null) // (optional) if not null, a different name can be specified from baseName
{
ResXResourcesDir = "Resources", // The subfolder of .resx resources. Default is "Resources"
Source = ResourceManagerSources.CompiledAndResX // Both types of resources are used. Default is CompiledAndResX
};
// If no .resx file exists yet the results will come purely from compiled resources
Console.WriteLine("Results before adding some content:");
Console.WriteLine(resourceManager.GetString("String1", enUS)); // from en because en-US does not exist
Console.WriteLine(resourceManager.GetString("String1", en)); // from en
Console.WriteLine(resourceManager.GetString("String1", inv)); // from invariant
// Adding some new content
resourceManager.SetObject("String1", "Replaced content in invariant culture", inv); // overriding existing compiled entry
resourceManager.SetObject("NewObject", 42, inv); // completely newly defined entry
resourceManager.SetObject("String1", "Test string in lately added American English resource file", enUS);
Console.WriteLine();
Console.WriteLine("Results after adding some content:");
Console.WriteLine(resourceManager.GetString("String1", enUS)); // from en-US resx
Console.WriteLine(resourceManager.GetString("String1", en)); // from compiled en
Console.WriteLine(resourceManager.GetString("String1", inv)); // from invariant .resx
// Removing works for .resx content. Removing an overridden entry resets the original compiled content.
resourceManager.RemoveObject("String1", inv);
// But by setting null explicitly even a compiled value can be suppressed.
// By removing the null entry the compiled content will re-appear
resourceManager.SetObject("String1", null, en);
Console.WriteLine();
Console.WriteLine("Results after deleting some content:");
Console.WriteLine(resourceManager.GetString("String1", enUS)); // from en-US .resx
Console.WriteLine(resourceManager.GetString("String1", en)); // from invariant because en is overridden by null
Console.WriteLine(resourceManager.GetString("String1", inv)); // from original invariant because .resx is removed
// By saving the resources the changes above can be persisted
// resourceManager.SaveAllResources();
}
}
// A possible result of the example above (depending on the created resource files and the added content)
// Results before adding some content:
// Test string in compiled en resource
// Test string in compiled en resource
// Test string in compiled invariant resource
//
// Results after adding some content:
// Test string in lately added American English resource file
// Test string in compiled en resource
// Replaced content in invariant culture
//
// Results after deleting some content:
// Test string in lately added American English resource file
// Test string in compiled invariant resource
// Test string in compiled invariant resource
Considering there are .resx files in the background not just String and other Object resources can be obtained by GetString and GetObject methods but metadata as well by GetMetaString and GetMetaObject methods. Please note that accessing aliases are not exposed by the HybridResourceManager class, but you can still access them via the IExpandoResourceSet type returned by the GetExpandoResourceSet method.
Similarly to ResXResourceSet, ResXResourceReader and ResXResourceManager, the HybridResourceManager class also has a SafeMode property, which changes the behavior of GetString/GetMetaString and GetObject/GetMetaObject methods:
While ResourceManager is read-only and works on binary resources, HybridResourceManager supports expansion (see IExpandoResourceManager) and works both on binary and XML resources (.resx files).
Incompatibility with ResourceManager:
New features and improvements compared to ResourceManager:
HybridResourceManager(Type, String) | Creates a new instance of HybridResourceManager class that looks up resources in compiled assemblies and resource XML files based on information from the specified type object. |
HybridResourceManager(String, Assembly, String) | Creates a new instance of HybridResourceManager class that looks up resources in compiled assemblies and resource XML files based on information from the specified baseName and assembly. |
CloneValues |
Gets or sets whether GetObject and GetMetaObject methods return always a new copy of the stored values.
Default value: . |
IgnoreCase |
Gets or sets a value that indicates whether the resource manager allows case-insensitive resource lookups in the
GetString/GetMetaString
and GetObject/GetMetaObject methods.
Default value: . (Overrides ResourceManagerIgnoreCase) |
IgnoreResXParseErrors |
Gets or sets whether .resx file errors should be ignored when attempting to load a resource set. If ,
then non-loadable resource sets are considered as missing ones; otherwise, an exception is thrown.
Default value: . |
IsDisposed | Gets whether this HybridResourceManager instance is disposed. |
IsModified | Gets whether this HybridResourceManager instance has modified and unsaved data. |
NeutralResourcesCulture | Gets the CultureInfo that is specified as neutral culture in the Assembly used to initialized this instance, or the CultureInfo.InvariantCulture if no such culture is defined. |
ResXResourcesDir |
Gets or sets the relative path to .resx resource files.
Default value: Resources |
SafeMode |
Gets or sets whether the HybridResourceManager works in safe mode. In safe mode the retrieved
objects returned from .resx sources are not deserialized automatically.
Default value: . |
Source |
Gets or sets the source, from which the resources should be taken.
Default value: CompiledAndResX |
ThrowException |
Gets or sets whether a MissingManifestResourceException should be thrown when a resource
.resx file or compiled manifest is not found even for the neutral culture.
Default value: . |
Dispose | Disposes the resources of the current instance. |
Dispose(Boolean) | Releases the resources used by this HybridResourceManager instance. |
GetExpandoResourceSet | Retrieves the resource set for a particular culture, which can be dynamically modified. |
GetMetaObject | Returns the value of the specified non-string metadata for the specified culture. |
GetMetaStream | Returns a MemoryStream instance from the metadata of the specified name and culture. |
GetMetaString | Returns the value of the string metadata for the specified culture. |
GetObject(String) |
Returns the value of the specified resource.
(Overrides ResourceManagerGetObject(String)) |
GetObject(String, CultureInfo) |
Gets the value of the specified resource localized for the specified culture.
(Overrides ResourceManagerGetObject(String, CultureInfo)) |
GetResourceSet |
Retrieves the resource set for a particular culture.
(Overrides ResourceManagerGetResourceSet(CultureInfo, Boolean, Boolean)) |
GetStream(String) | Returns a MemoryStream instance from the resource of the specified name. |
GetStream(String, CultureInfo) | Returns a MemoryStream instance from the resource of the specified name and culture. |
GetString(String) |
Returns the value of the specified string resource.
(Overrides ResourceManagerGetString(String)) |
GetString(String, CultureInfo) |
Returns the value of the string resource localized for the specified culture.
(Overrides ResourceManagerGetString(String, CultureInfo)) |
InternalGetResourceSet |
Provides the implementation for finding a resource set.
(Overrides ResourceManagerInternalGetResourceSet(CultureInfo, Boolean, Boolean)) |
ReleaseAllResources |
Tells the resource manager to call the ResourceSet.Close method on all ResourceSet objects and release all resources.
All unsaved resources will be lost.
(Overrides ResourceManagerReleaseAllResources) |
RemoveMetaObject | Removes a metadata object from the current HybridResourceManager with the specified name for the specified culture. |
RemoveObject | Removes a resource object from the current HybridResourceManager with the specified name for the specified culture. |
SaveAllResources | Saves all already loaded resources. |
SaveResourceSet | Saves the resource set of a particular culture if it has been already loaded. |
SetMetaObject | Adds or replaces a metadata object in the current HybridResourceManager with the specified name for the specified culture. |
SetObject | Adds or replaces a resource object in the current HybridResourceManager with the specified name for the specified culture. |
Convert |
Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example. (Defined by ObjectExtensions) |
ConvertTTarget |
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions) |
In |
Gets whether item is among the elements of set.
See the Examples section of the generic InT(T, T) overload for an example. (Defined by ObjectExtensions) |
TryConvert |
Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. (Defined by ObjectExtensions) |
TryConvert |
Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. (Defined by ObjectExtensions) |
TryConvertTTarget |
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. (Defined by ObjectExtensions) |
TryConvertTTarget |
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. (Defined by ObjectExtensions) |