1

The context: we are currently using a solution where all localizable strings are in XAML files which are translated. For translating strings in code, we use a function that will search from associated resource dictionary:

MessageBox.Show(this.i18n("my message"));

I would like to implement a code analyzer that will check if the "my message" string is actually declared in associated XAML file. The problem is that I can't find anything in compilation context that would lead me to the correct XAML file.

If the resource management is outside of the scope for Roslyn I could use DTE Interface for my task but I would like to know if there are better solutions for it.

jv42
  • 8,521
  • 5
  • 40
  • 64
  • Roslyn is about C# code only. You need to use VS APIs directly, and you need to figure out how to rescan when the XAML is changed. – SLaks Sep 04 '15 at 14:17
  • @SLaks: we offer a way to specify files (like the XAML files in this case) that you need the content of in an analyzer. Yes, we don't help you interpret the contents of them, but I think Robert here is asking just how to get to the content in the first place. – Jason Malinowski Sep 04 '15 at 17:48

1 Answers1

4

Roslyn exposes an AdditionalFiles mechanism where you can specify some additional files to be passed into your analyzer which you need the content of. XAML files for what you're doing would be a perfect example. We have one Roslyn analyzer that we run on Roslyn itself that verifies that the types we have in our API match an additional file (called PublicAPI.Shipped.txt). If you look at this as a sample it'll show you how to read in extra files.

This doesn't give you any help at interpreting the files (you'll need to parse them yourself), but this at least gives you the mechanism to get the contents of them. We'll take care of all the mucking around reading the file from disk and everything for you.

You still have to specify that you actually want the files to be included in the AdditionalFiles list in the first place. If you look here you can see that you can specify an MSBuild item group name that will get passed through everything.

Jason Malinowski
  • 18,148
  • 1
  • 38
  • 55
  • 1
    How would you rerun the analyzer on XAML changes? – SLaks Sep 04 '15 at 19:33
  • Thank You! Seems to be just what I need. SLaks, it seems like the files are loaded every time the program is compiled which is perfect for me. – Robert Hudjakov Sep 05 '15 at 04:57
  • Not getting my head around whether AdditionalFiles means additional files in the analyzer assembly or the assembly being analyzed. I need external data which should come with the analyzer but not the target being analyzed. – Thomas Zeman Feb 28 '17 at 12:06
  • AdditionalFiles are for the target being analyzed. If you need to find files relative to your analyzer, you could do something like typeof(MyAnalyzer).Assembly.Location to get the path and do stuff from there. I forget what the portable equivalent of that is though... – Jason Malinowski Mar 01 '17 at 07:04
  • 1
    Links are dead by now, but this should answer all questions: https://github.com/dotnet/roslyn/blob/master/docs/analyzers/Using%20Additional%20Files.md – Kris Vandermotten May 12 '17 at 14:00
  • I would like to analyze WPF XAML files. Analyzing all of them takes minutes, but what I really want anyway is immediate feedback for the developer in the file they are currently editing. Is there any good way to do this on the .NET Compiler Platform? – Paul Jun 27 '19 at 10:00