【问题标题】:How do you programmatically run Static Code Analysis in Visual Studio 2017?如何在 Visual Studio 2017 中以编程方式运行静态代码分析?
【发布时间】:2018-05-29 19:39:46
【问题描述】:

我正在研究一种本地化遗留应用程序的解决方案。我使用 EnvDte 编写了一个 Visual Studio 插件,它可以自动将解决方案中每个表单的“可本地化”标志设置为 true,这是在表单设计器上提取资源的关键步骤。我现在正在尝试处理任何以编程方式设置的文本,即触发Globalization (CA13##) 警告的文本​​。

designer.Visible = true;
var host = (IDesignerHost)designer.Object;
var provider = TypeDescriptor.GetProvider(host.RootComponent);
var typeDescriptor = provider.GetExtendedTypeDescriptor(host.RootComponent);
if (typeDescriptor == null)
    continue;

var propCollection = typeDescriptor.GetProperties();
var propDesc = propCollection["Localizable"];
if (propDesc != null && host.RootComponent != null &&
    (bool?)propDesc.GetValue(host.RootComponent) != true)
{
    try
    {
        propDesc.SetValue(host.RootComponent, true);
    }
    catch (Exception ex)
    {
        // log the error
    }

    // save changes
}

我已经能够从菜单中手动运行它,使用:分析 -> 运行代码分析 -> 解决方案 以获取问题列表,但我想自动执行此步骤另一个运行并提取结果的加载项。

是否有任何资源指向访问构建警告或代码分析结果?

有没有使用 EnvDte 或 Roslyn 的解决方案?

【问题讨论】:

    标签: visual-studio envdte static-code-analysis roslyn-code-analysis


    【解决方案1】:

    好的,我已经设法收集到足够的信息来组合加载项。简单来说,你使用_dte.ExecuteCommand()

    1. 初始化命令:

      // nothing to see here...
      _package = package ?? throw new ArgumentNullException(nameof(package));
      
      var commandService = ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
      if (commandService == null)
          return;
      _dte = (DTE2)ServiceProvider.GetService(typeof(DTE));
      var menuCommandId = new CommandID(CommandSet, CommandId);
      var menuItem = new MenuCommand(MenuItemCallback, menuCommandId);
      commandService.AddCommand(menuItem);
      
      _events = _dte.Events.BuildEvents;
      // since static code analysis is a sort of build you need to hook into OnBuildDone
      _events.OnBuildDone += OnBuildDone;
      
    2. 触发分析

      private void MenuItemCallback(object sender, EventArgs e)
      {
          _dte.ExecuteCommand("Build.RunCodeAnalysisonSolution");
      }
      
    3. 在 OnBuildDone 事件中提取错误

      private void OnBuildDone(vsBuildScope scope, vsBuildAction action)
      {
          Dispatcher.CurrentDispatcher.InvokeAsync(new Action(() =>
          {
              _dte.ExecuteCommand("View.ErrorList", " ");
              var errors = _dte.ToolWindows.ErrorList.ErrorItems;
              for (int i = 1; i <= errors.Count; i++)
              {
                  ErrorItem error = errors.Item(i);
                  var code = error.Collection.Item(1);
                  var item = new
                  {
                      error.Column,
                      error.Description,
                      error.ErrorLevel,
                      error.FileName,
                      error.Line,
                      error.Project
                  };
                  error.Navigate(); // you can navigate to the error if you wanted to.
              }
          });
      }
      

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 2023-04-08
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 2019-09-05
      • 2015-11-17
      • 1970-01-01
      相关资源
      最近更新 更多