Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to build Roslyn Analyzer targeting .NET Standard \ Core rather than .NET Portable

I'm using default Roslyn SDK templates that came with Visual Studio 2017. The projects they create target .NET Framework Portable. I'm assuming Roslyn extensibility projects can target .NET Standard \ Core instead of Portable and I'm looking for templates or a sample of Roslyn Analyzer \ Refactoring project that I could study.

like image 972
Ivan Koshelev Avatar asked Aug 27 '17 00:08

Ivan Koshelev


Video Answer


1 Answers

Sample of converted analyzer from default analyzer template is available here. There is original analyzer for comparison along with TestAnalyzerStandard which targets .NET standard.

Steps to make it work:

  • Create new .NET Standard library
  • Library must target .NET Standard 1.3. This is required if you wish to run analyzer as extension inside VS (extensions target .NET 4.6). Mapping between standard versions and full framework versions is available here. Also if you try to target lower version than 1.3, you will not be able to include required analyzer packages.
  • Add nuget package for Microsoft.Composition latest version. This is needed by Microsoft.CodeAnalysis.CSharp.Workspaces. If you try to add workspaces first, you will get error that referenced composition package is not compatible.
  • Add nuget package for Microsoft.CodeAnalysis.CSharp (I'm using latest 1.* version)
  • Add nuget package for Microsoft.CodeAnalysis.Csharp.Workspaces (version should match the version of Microsoft.CodeAnalysis.CSharp).
  • At this point you can copy code from portable project and build it. There should be no errors (you may have to close and reopen solution if VS is still displaying red squiggles).
  • To make VS extension work, just open source.extension.vsixmanifest, go to assets tab and change reference to .NET standard library
  • To create .nuget package just execute nuget pack Diagnostic.nuspec .. Diagnostic.nuspec is valid for Nuget 2.x. If you are using nuget via package management console in VS 2017 you will have to change <file src="*.dll" ..." to <file src="bin\*\netstandard1.3\*.dll" ....

Those steps are result of my experimentation with analyzers (I previously played with creating DLL which targeted full framework instead of being portable library). They are not by any means official.

like image 62
nejcs Avatar answered Oct 07 '22 01:10

nejcs