Sunday, September 6, 2015

Visual Studio: Making a Resource File (resx) Public Versus Internal (default)

I was developing a project that was a WPF desktop application which included various resources (text, icons, images, etc.). I knew that there was potential to develop another application that could also make use of the resources. The issue is that by default the class generated by a resource file is created with the access modifier set to internal.  Setting the class to be internal means that only files within the same assembly can see the data type defined by the class.

The project was organized as follows:



The EpicApplicationMaster solution contains two projects:
  • EpicClassLibrary: a class library (a.k.a. an assembly) to potentially be shared by multiple applications.
    • This class library contains a resource file named, ResourcesThatWouldBeSwellToShare.resx
  • EpicWPFApplication: a stock WPF application that references the EpicClassLibrary project.

The previous screenshot demonstrates two resources (ScaryPurpleDinosaur and MotherlyAdvice) that could be shared with other applications.

Within Solution Explorer it is possible to double click on the C# file, ResourcesThatWouldBeSwellToShare.designer.cs, associated with resource file, ResourcesThatWouldBeSwellToShare.resx. This reveals the C# code generated based on the resources defined:



The class defined in the previous image is specified as internal (internal class ResourcesThatWouldBeSwellToShare) which means the WPF project does not have access to any of the defined resources.

The ResourcesThatWouldBeSwellToShare.designer.cs is generated by a custom build tool that takes as input the resource file, ResourcesThatWouldBeSwellToShare.resx, and generates the corresponding C# code. To see this tool right click on ResourcesThatWouldBeSwellToShare.resx from within Solution Explorer and select the Properties item from the context menu:



Selecting Properties from the ResourcesThatWouldBeSwellToShare.resx context menu displays the following:



The value for the "Custom Tool" property is ResXFileCodeGenerator which is the tool responsible for generating the C# class with the internal access modifier. There is no way to pass a command-line value to ResXFileCodeGenerator in order to tell it to generate public as the class' access modifier.

To create a resource files with the class generated contains public as the access modifier replace the ResXFileCodeGenerator custom tool with PublicResXFileCodeGenerator:



The name of PublicResXFileCodeGenerator indicates that it creates public rather than internal classes from resx files. Building the code following the aforementioned change generates a public class:




No comments :

Post a Comment