需要构建针对.NET Standard \ Core而不是.NET Portable的Roslyn Analyzer

时间:2017-08-27 00:46:51

标签: .net-core roslyn analyzer .net-standard

我正在使用Visual Studio 2017附带的默认Roslyn SDK模板。他们创建的项目面向.NET Framework Portable。我假设Roslyn可扩展性项目可以针对.NET Standard \ Core而不是Portable,我正在寻找模板或我可以研究的Roslyn Analyzer \ Refactoring项目示例。

2 个答案:

答案 0 :(得分:9)

Sample of converted analyzer from default analyzer template is available here. There is original analyzer for comparison along with Object 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 String latest version. This is needed by 'This contains the list of keys that should be disabled from interacting with your check boxes. 'You can add or remove keys from this as you like. Dim DisabledKeys As New HashSet(Of Keys) From { Keys.Enter, Keys.Space } Private Sub CheckBoxes_KeyDown(sender As Object, e As KeyEventArgs) Handles CheckBox1.KeyDown, CheckBox2.KeyDown, CheckBox3.KeyDown e.SuppressKeyPress = DisabledKeys.Contains(e.KeyCode) End Sub . If you try to add workspaces first, you will get error that referenced composition package is not compatible.
  • Add nuget package for TestAnalyzerStandard (I'm using latest 1.* version)
  • Add nuget package for Microsoft.Composition (version should match the version of Microsoft.CodeAnalysis.CSharp.Workspaces).
  • 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 Microsoft.CodeAnalysis.CSharp, go to assets tab and change reference to .NET standard library
  • To create .nuget package just execute Microsoft.CodeAnalysis.Csharp.Workspaces. Microsoft.CodeAnalysis.CSharp is valid for Nuget 2.x. If you are using nuget via package management console in VS 2017 you will have to change source.extension.vsixmanifest to nuget pack Diagnostic.nuspec ..

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.

答案 1 :(得分:5)

我开始研究一个新的Roslyn项目并逐个构建,而不是使用模板。 https://github.com/IKoshelev/Roslyn.AutoLogging/commit/1f88e3e49141e0fa425c51fdcb3457a7c3d6dcaa

我设法进行了以下定位:

重构项目 - .NET Standard 1.3(此.dll将分发,版本保持最小)

UnitTests项目 - .NET Core 2.0

VSIX项目 - .NET Framework 4.6(我相信,只有完整的Visual Studio支持VSIX,所以没关系)

更新 Roslyn的版本控制现在有点复杂,即如果您想在Visual Studio 2015中使用扩展,则必须使用PCL库。更多信息,请参阅本文末尾article on Roslyn