.NET Roslyn:运行时配置 [英] .NET Roslyn : runtime configuration

查看:61
本文介绍了.NET Roslyn:运行时配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用Roslyn代码分析器来开发一些规则.此规则是为了控制名称空间的访问.例如,DAL只能使用核心.如果View使用DAL,我要警告.

我使用"Visual Studio 2015社区版"中的模板带有代码修复功能的分析器(NuGet + VSIX)"来生成插件.

我已经进行了一些测试,并且工作正常.但是,该规则是在代码中刻苦编写的.我不知道如何在运行时配置规则.最好是解决方案或项目中的配置文件.

能给我一些例子吗?

谢谢.

解决方案的恢复:

  1. 在分析的项目中的Visual Studio中添加文件.
  2. 使用编辑器打开.csproj,修改配置文件的项目组.

    < None Include ="test.txt"/> ->< AdditionalFiles Include ="test.txt"/>

设置此选项会将以下内容添加到您的.csproj:

 < ItemGroup>< AdditionalFiles Include ="test.txt"/></ItemGroup> 

您的分析器可以通过 AnalyzerOptions.AdditionalFiles ,可在 CompilationStartAnalysisContext.Options 中找到.

I am going to develop some rules with the Roslyn code analyzer. This rule is to control the access of a namespace. Example, the DAL can use only the core. If the View use the DAL, I want a warning.

I use the template "Analyzer with Code Fix (NuGet + VSIX)" in 'Visual Studio 2015 Community Edition' to generate the plugin.

I have made some test and it works fine. However the rule is written at hard in the code. I don't know how configure the rule in the runtime. The best will be a configuration file in the solution or the project.

Can you give me some example?

Thank.

Resume of solution :

  1. Add the file in Visual Studio in the project analyzed.
  2. Open the .csproj with a editor modify the item group of configuration file.

    <None Include="test.txt" /> -> <AdditionalFiles Include="test.txt" />

Example to read the file!

解决方案

You can add analyzer runtime configuration via AdditionalFiles items in the .csproj. They get expanded into /additionalfile:<path> command-line options for the compiler. In VS "15" preview you can do that from the UI:

Setting this adds the following to your .csproj:

<ItemGroup>
  <AdditionalFiles Include="test.txt" />
</ItemGroup>

Your Analyzer can accesses them via AnalyzerOptions.AdditionalFiles, available in CompilationStartAnalysisContext.Options.

这篇关于.NET Roslyn:运行时配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆