run StyleCop
使用StyleCop时,我总是做菜单或构建项目。
保存程序后,我想运行StyleCop。
是否有可能?
不幸的是,宏已被删除,但是编写外接程序非常容易。首先,创建一个新的C#加载项项目(完成后,您需要将DLL部署到Visual Studio AddIns文件夹中,然后重新启动VS)。
编辑生成的模板以附加到DocumentSaved
事件:
private DocumentEvents _documentEvents;
public void OnConnection(object application,
ext_ConnectMode connectMode,
object addInInst,
ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
_documentEvents = _applicationObject.Events.DocumentEvents;
_documentEvents.DocumentSaved += DocumentEvents_DocumentSaved;
}
public void OnDisconnection(ext_DisconnectMode disconnectMode,
ref Array custom)
{
_documentEvents.DocumentSaved -= DocumentEvents_DocumentSaved;
}
您的DocumentEvents_DocumentSaved
方法只需要调用正确的VS命令(请注意,命令名称随您使用的Visual Studio版本而异)。
private void DocumentEvents_DocumentSaved(Document Document)
{
document.DTE.ExecuteCommand("Build.RunCodeAnalysisonSelection", "");
}
在这种情况下,您将仅在当前项目上运行代码分析(假设这是您保存的内容,然后它也是您要测试的内容)。此假设对于“全部保存”失败,因此您可能需要使用"Build.RunCodeAnalysisonSolution"
。当然,还有很多改进空间(例如,当发生多个接近连续的保存时)。
如果您以VS 2013为目标,则不应使用AddIns,因为不推荐使用AddIns,而推荐使用Packages。您有相同的事情要做,但是您可以通过收到该通知IVsRunningDocTableEvents
。覆盖Initialize()
您的Package
(将实现IVsRunningDocTableEvents
接口)。AdviseRunningDocTableEvents()
从IVsRunningDocumentTable
(通过获取GetService()
)致电,您已完成。
最后请注意,相同的技术也适用于任何其他事件(成功构建之后,部署之前,关闭解决方案时等等)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句