Extract literal strings from assembly
-
22-06-2021 - |
Domanda
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.
Soluzione
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"
...
Altri suggerimenti
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
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.