How to override default System.Resources.ResourceManager in Resources.Designer.cs?
I believe you're asking two separate questions here. You can certainly override ResourceManager.GetString
. You can't, however, use that override in the auto-generated Resource.Designer.cs
code. To use it, you'll have to write your own Resource
designer class.
public class MyResourceManager : System.Resources.ResourceManager
{
// override
public override GetString(string name)
{
// custom code
}
}
public class MyResourceDesigner
{
// use your custom class with override
private static MyResourceManager resourceManager;
private static CultureInfo resourceCulture;
public static MyResourceManager ResourceManager
{
get
{
if (object.ReferenceEquals(resourceManager, null))
{
// Resource is just the name of the .resx file
// be sure to include relevant namespaces
var temp = new MyResourceManager(
"MyProject.Resource",
typeof(MyResourceDesigner).Assembly);
resourceManager = temp;
}
return resourceManager;
}
}
public static CultureInfo Culture
{
get
{
return resourceCulture;
}
set
{
resourceCulture = value;
}
}
// start adding strongly-typed objects
public static string Foo
{
get
{
// use your override
return ResourceManager.GetString("Foo", resourceCulture);
}
}
}
I created a CustomResourceManager
that overrides the GetString()
calls like so:
public class CustomResourceManager : ResourceManager
{
public CustomResourceManager(Type resourceSource)
:base(resourceSource)
{
}
public CustomResourceManager(string baseName, Assembly assembly)
: base(baseName, assembly)
{
}
public CustomResourceManager(string baseName, Assembly assembly, Type usingResourceSet)
: base(baseName, assembly, usingResourceSet)
{
}
public override string GetString(string name)
{
// your business logic
}
public override string GetString(string name, CultureInfo culture)
{
// your business logic
}
}
Then I added a pre-build step to my project to replace the creation of a System.Resources.ResourceManager
with my CustomResourceManager
in the generated Resource.Designer.cs
file:
powershell -command "(gc ..\Resources\Resource.Designer.cs).Replace(\"new global::System.Resources.ResourceManager\", \"new CustomResourceManager\") | set-content ..\Resources\Resource.Designer.cs -Encoding UTF8"
And away we go!