yangzn

VS 小插件编写

本示例把当前文件的 属性的语义注释, 换成[Dispaly(Name="***")]的样式. 

用处: 有很多Export To Excel的Dto对象,需要用Display Attribute显示输出的列名,一个一个的改太麻烦了.

1.新建 vsix工程.

2.参照 https://docs.microsoft.com/en-us/visualstudio/extensibility/adding-a-menu-to-the-visual-studio-menu-bar?view=vs-2019

创建一个菜单项.

3.在命令的Provider.cs中加入

using Microsoft.VisualStudio.Shell;

using System.ComponentModel.Composition;
using EnvDTE80;

 

并改写 Exeute方法

 private void Execute(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            var dte = Package.GetGlobalService(typeof(DTE)) as DTE2;

            var oFind = dte.Find;
            oFind.FindWhat = "(/// <[s/].*)|(\\[.*)";
            oFind.ReplaceWith = "";
            oFind.MatchCase = false;
            oFind.MatchWholeWord = false;
            oFind.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr; //regex
            oFind.Target = vsFindTarget.vsFindTargetCurrentDocument; // vsFindTarget.vsFindTargetCurrentDocument;
            oFind.Action = vsFindAction.vsFindActionReplaceAll;
            oFind.Execute();
            oFind.FindWhat = "/// (.*)\\b";
            oFind.ReplaceWith = "[Display(Name=\"$1\")]";
            oFind.Execute();

        }

  做完了,才发现有个 Macros for Visual studio 也可以做同样的事.

只是代码稍改动一下就可以.

dte.ExecuteCommand("Edit.Replace");
var oFind = dte.Find;
oFind.FindWhat = "(/// <[s/].*)|(\\[.*)";
oFind.ReplaceWith = "";
oFind.MatchCase = false;
oFind.MatchWholeWord = false;  
oFind.PatternSyntax = 1; //regex
oFind.Target = 1; // vsFindTarget.vsFindTargetCurrentDocument;

oFind.Action = 4; //vsFindAction.vsFindActionReplaceAll;

oFind.Execute();
oFind.FindWhat = "/// (.*)\\b";
oFind.ReplaceWith = \'[Display(Name="$1")]\';
oFind.Action = 4; //vsFindAction.vsFindActionReplaceAll;
oFind.Execute();

  

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
  • 2021-08-09
  • 2022-02-10
  • 2021-08-04
  • 2021-09-14
  • 2021-06-18
猜你喜欢
  • 2022-12-23
  • 2021-10-17
  • 2022-12-23
  • 2021-08-19
  • 2021-10-04
  • 2021-06-09
  • 2021-11-18
相关资源
相似解决方案