komodosp
komodosp

Reputation: 3616

ResXFileCodeGenerator - overriding the output (want to use a Custom ResourceManager)

I've created a class the inherits from ResourceManager. My problem is that if I change my Resources.Designer.cs to use it,

e.g.

private static global::System.Resources.ResourceManager _resourceManager;

to

private static global::MyProject.Resources.MyResourceManager _resourceManager;

it gets overwritten by the ResXFileCodeGenerator and set back to the default System.Resources.ResourceManager.

I don't want to turn off the ResXFileCodeGenerator. But is there any way of telling it which ResourceManager class to use?

I was told to look at ResXFileCodeGeneratorEx but can't really see how that is any different in this respect.

Upvotes: 5

Views: 6981

Answers (3)

RWC
RWC

Reputation: 5052

@James Woodall: I had to change your code a little bit to have it work with the correct language file:

var innerField = typeof(Resources.Resources).GetField("resourceMan",
BindingFlags.NonPublic | BindingFlags.Static);

innerField.SetValue(null,
    new ExtendedResourceManager("MyNamespace.ResourceFileWithoutResx",
        typeof(Resources.Resources).Assembly));

innerField = typeof(Resources.Resources).GetField("resourceCulture",
BindingFlags.NonPublic | BindingFlags.Static);

innerField.SetValue(null, CultureInfo.CurrentUICulture); 

Thank you for solution, which is imho a far better solution than the accepted answer, which has too many disadvantages.

Upvotes: 0

James Woodall
James Woodall

Reputation: 735

A little belated but using reflection you can fix this. I put this code in the Startup.cs file.

var innerField = typeof(Resources.Resources).GetField("resourceMan",
    BindingFlags.NonPublic | BindingFlags.Static);

if (innerField != null)
{
    innerField.SetValue(null,
        new ExtendedResourceManager("MyNamespace.ResourceFileWithoutResx",
            typeof(Resources.Resources).Assembly));
}

Because the designer generated file is there for all to see, simply modify the code I've written based on the ResourceManager { get; } code generated for you.

You can create your own ExtendedResourceManager class and override all the properties as you wish.

Happy coding.

Upvotes: 7

komodosp
komodosp

Reputation: 3616

I managed to achieve this using T4

https://msdn.microsoft.com/en-us/library/bb126445.aspx

Basically allows automatic code generation in the way that ResXFileCodeGenerator does.

It's not a perfect solution though as it was a lot of work just to replicate the functionality of ResXFileCodeGenerator and also it doesn't automatically run every time the .resx file is saved without having to install another Visual Studio custom tool which I will have to give to get all the developers on the project to install.

Upvotes: 0

Related Questions