本示例把当前文件的 属性的语义注释, 换成[Dispaly(Name="***")]的样式.
用处: 有很多Export To Excel的Dto对象,需要用Display Attribute显示输出的列名,一个一个的改太麻烦了.
1.新建 vsix工程.
创建一个菜单项.
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();
原文:https://www.cnblogs.com/yangzn/p/11437775.html