Use it or lose it! [New Delay.FxCop code analysis rule helps identify unused resources in a .NET assembly]


This blog has moved to a new location and comments have been disabled.

All old posts, new posts, and comments can be found on The blog of dlaa.me.

See you there!

Comments (11)

  1. Isawen says:

    Is there a way to ignore the *.resx associated to forms and controls which have the Localize property set to True?

    Thanks

  2. Delay says:

    Isawen,

    I'm not sure which property you're referring to – if you have a link you can share, that might be helpful. That said, RESX files tend to be 1-per-assembly rather than 1-per-control, so I'm not sure how easy it would be to do what you're asking. But more information would definitely be appreciated! :)

  3. Isawen says:

    Let's assume you have created a standard Windows Forms application, with one button and one label on the form. Make the form localizable (set Localize attribute to True). The Form1 component is composed of Form1.cs, Form1.Designer.cs and Form1.resx. The Delay.FxCop rules are also displaying that the strings starting with >> (e.g >>button1.Name, or >>label1.Parent) from the Form1.resx are not used. Which is false, those resources apply using the following command which is found in the generated Form1.Designer.cs "resources.ApplyResources(this.button1, "button1");"

    And RESX files not tend to be 1 per assembly. Placing all messages into a single RESX file is not a good approach. I like to have resources by components, much easier to track them when I need something.

    Regards.

  4. Delay says:

    Isawen,

    Thank you very much for the clarification – it's been many years since I did Windows Forms development and it sounds like some of the conventions there differ from my more recent WPF/Silverlight experience. The explanation you provide is great and it sounds like this scenario is a definite candidate for improvement. I've updated my TODO list and hope to get to this in the near-ish future!

  5. Isawen says:

    Hello David,

    Were you able to improve your FxCop rule to detect the unused strings from resources?

    Thanks.

  6. Delay says:

    Isawen,

    Not yet, sorry. I haven't been riding the bus to work lately and that's when I do most of the work for my blog. :) I'm hoping things will settle down in a week or two!

  7. Jason says:

    Ruleset dll throws an exception when you have a const string set to null

    for example

    public const string MyString = null;

  8. Delay says:

    Jason,

    Sorry about that! I've just made a fix and uploaded a new ZIP with the updated code+assembly (the version number is now 1.0.0.2). Please re-download and let me know if that fixes the problem for you.

    Thanks!

  9. Jason says:

    thanks, fixed the problem.

    one minor bug and one request if it is easy.

    bug: I don't know what you are filling in for Help URL, but fxcop says "Malformed URL" and if you click the link on the Rule Support tab, it causes fxcop to crash badly.

    (In truth it is more an fxcop bug since they shouldn't crash like this even if you give a bad url)

    since you don't actually have a help page for this maybe about:blank would make it not crash.

    The minor request is on the spell check of literals, can you display the full string? (or at least X chars around the misspelled word)

    in any case, thanks a bunch.

    -J

  10. Delay says:

    Jason,

    Thanks for getting back to me! I've added a note to my TODO list for your idea for adding context around misspelled literals. I'll look into the Help URL thing soon because it sounds like it should be a quick enough fix. I'll reply back here with the results (good or bad).

    I appreciate the feedback – thank you!

  11. Delay says:

    Jason,

    I've made a tweak to populate the URL field of each rule with a link to this blog. :) I've uploaded a new ZIP with the updated code+assembly (version number 1.0.0.3) – please give it a try.

    Thanks again for your help!