0

I have a large Resources.resx file in my application and I'm accessing them with

MyResources.GetString("res1");

How to check that for every entry "Entry" in the .resx file exists at least one call MyResources.GetString("Entry") (which means every resource is used somewhere)

, and how to check that for every call MyResources.GetString("Entry") exists a resource with the name "Entry" (there are no call to nonexisting resources) ?

Dimitar Tsonev
  • 3,711
  • 5
  • 40
  • 70
  • What exactly are you trying to achieve? If you need to get rid of unused resources - that can be a hard one. I do not know of an easy way (except code searching) that will say if you are using a certain resource. For the second requirement, I'd recommend using a wrapper of the resource retrieval that would handle the absence of the resource in a safe manner. – Ivaylo Slavov May 14 '13 at 10:44
  • If you use wrapper solution, you may add Debug.Assert calls that check that resource exists. Each time unused resource is referenced in Debug mode the Assertion Failed window will pop up. – Artemix May 14 '13 at 11:04
  • Maybe this is what you want? A tool to check unused resx entry https://stackoverflow.com/a/26598684 – Tonghua Apr 30 '18 at 20:13

1 Answers1

0

It is possible, but fiddly, to discover unused resources:

(1) Ensure Code Analysis warning CA1811 is turned on.

(2) In your Resources.Designer.CS file (or other applicable designer file) comment out the following lines:

[global:: System.CodeDom.Compiler.GeneratedCodeAttribute( ...
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global:: System.Runtime.CompilerServices.CompilerGeneratedAttribute()]

(3) Change the class to a static class by putting the word "static" in front of "class":

 internal static class Resources {

(4) Comment out the default constructor.

(5) Comment out any “InternalsVisibleTo” lines (these are usually in “AssemblyInfo.cs”).

(6) Compile your program. Now you should be warned about any unused resources.

(7) Remove your unused resources. (Note that changing any resources will regenerate the Resources.Designer.cs file.)

I'm not sure how to find undefined resources though.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276