2

Not quite sure where to ask this.

Basically, I handle localization for our product. I need a way to extract all literal strings from compiled assemblies. I need to get the line number and source file (for comparison purposes). I've been using FxCop, and I just created a rule to spit out all of the "ldstr" instructions. But, this relies on FxCop which is obsolete (I believe). I also considered using ildasm, but I can't find any tutorials on how to parse the output to reliably retrieve the information I need.

So, is there a utility to handle this? A simple way I'm not thinking of? I'd like to avoid extensions like ReSharper as well because it only operates on the current state of your solution; e.g. I can't produce output to use for comparison.

xallean
  • 21
  • 1
  • 5
  • Yes I have the source. I'd need to have the source in order to do localization....right? Once I find the literal strings, I add them to the appropriate .resx file. – xallean Jul 20 '12 at 18:36

3 Answers3

2

ildasm can do that:

ildasm.exe /text /metadata=heaps mscorlib.dll >out.txt

// User Strings
// -------------------------------------------------------
// 70000001 : ( 4) L"info"
// 7000000b : ( 2) L", "
// 70000011 : ( 5) L"value"
// 7000001d : ( 1) L"D"
...
Feng Yuan
  • 709
  • 5
  • 4
1

If you are using Visual Studio you can run the following macro

Sub TemporaryMacro()
        DTE.ExecuteCommand("Edit.FindinFiles")
        DTE.Find.FindWhat = ":q"
        DTE.Find.Target = vsFindTarget.vsFindTargetFiles
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
        DTE.Find.SearchPath = "C:\yourproject"
        DTE.Find.SearchSubfolders = True
        DTE.Find.FilesOfType = "*.*"
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1
        DTE.Find.Action = vsFindAction.vsFindActionFindAll
    End Sub

Basically it is doing a regular expression search of :q quoted strings in a directory that will give you all quoted strings in your source code

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
  • This is a nice backup solution! I didn't know I could do this. Would there be a way to export the output from this to a file containing the line number and source file for each ":q" found? – xallean Jul 20 '12 at 18:37
0

Clearly the awesome Cecil OSS framework could be useful here. It will let you find easily string literals in assembly file.

A bit more work is needed to find back the strings in source file. The astute would consist in searching in the content of textual source files of the assembly to find all string declaration:

  • Verbatim like @"my""string"
  • or classic like "my\"string"

The list of source file of the assembly can be obtained from the Cecil.PDB sub-framework.

Community
  • 1
  • 1
Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92