The Roslyn Report: Writing Diagnostic Analyzers for Xamarin - Part 1

The Task:

One of the most important thing when developing iOS and Xamarin are the UIView and the UIViewController with their numereous events. All the wiring of logic and UI and also the layouting happens here. Even when you use Frameworks like MVVM or XIBs you are dependant to the flow of LoadView and ViewWillDisappear. Adam Kemp has an excellent blog describing what to do on which stage of the iOS Layout process: http://blog.adamkemp.com/2014/11/ios-layout-gotchas-and-view-controller.html

But how can one controll the code to obey the rules Adam described in his blog? Enter Roslyn - the new Microsoft compiler engine with the possibility to write own code diagnosis routines! See the github page for additional doku and the MSDN site for a reference.

The Setup:

What do we need to get Roslyn going? The easiest way is to use the current Azure Visual Studio 2015 VM. Additionaly we need:

After the setup you should have a running VS2015 with this new project template:

When you create a project you get a solution with the following structure:

  • Analyzer (Portable): The analyzer
  • Analyzer.Test: Unit tests for the analyzer
  • Analyzer.Vsix: A Visual Studio installer for the analyzer.

A diagnostic for Xamarin

Because Roslyn analyses code fragments with partial compilation the Xamarin.iOS.dll from C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Xamarin.iOS\v1.0\ is needed to compile and diagnose Xamarin code fragments. Copy it over from your development environment to the Azure VM. I would suggest to copy it to the same folder again. Then reference it from the Unit Test project to get it into build output directory and make it loadable by Roslyn.

In the next entry I will show how to write a Roslyn code analysis ...