17 April 2024

Tip: Using a .RESX resource in a class

We have seen in a previous article how to localize our application thanks to resources. Now here is a little trick to use these resources in a class where you can’t do dependency injection of the localization service.

Using ResourceManager

Just declare a variable of System.Resources.ResourceManger with 2 parameters:

  • baseName : The “path” of your targeted resource file
  • assembly

If my resource structure is like this:

Then the code will be :

System.Resources.ResourceManager rm = new( "PlanningWeb.Resources.PDF.report", System.Reflection.Assembly.GetExecutingAssembly() );
string myString = rm.GetString("myTitle");

Leave a Reply