How do I intercept a Visual Studio command execution
My third How do I is up.
Scenario
Let's say that we want to intercept the "Clean Solution" command. For a particular kind of project, before executing the command we need to clean other resources and instead of doing it with msbuild we want to do it by intercepting the command at the VS level. The focus of this How do I tackles this scenario.
Interfaces and classes needed
Code snippet
public class VSCommandInterceptor : IDisposable
{
private IServiceProvider serviceProvider;
private Guid guid;
private int id;
private bool isDisposed;
public VSCommandInterceptor(IServiceProvider serviceProvider, Guid commandGuid, int commandId)
{
this.serviceProvider = serviceProvider;
this.guid = commandGuid;
this.id = commandId;
if(CommandEvents != null)
{
CommandEvents.AfterExecute += new _dispCommandEvents_AfterExecuteEventHandler(OnAfterExecute);
CommandEvents.BeforeExecute += new _dispCommandEvents_BeforeExecuteEventHandler(OnBeforeExecute);
}
}
public event EventHandler<EventArgs> AfterExecute;
public event EventHandler<EventArgs> BeforeExecute;
private CommandEvents commandEvents;
protected CommandEvents CommandEvents
{
get
{
if(commandEvents == null)
{
DTE dte = this.serviceProvider.GetService(typeof(DTE)) as DTE;
if(dte != null)
{
commandEvents = dte.Events.get_CommandEvents(guid.ToString("B"), id) as CommandEvents;
}
}
return commandEvents;
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if(!this.isDisposed && disposing)
{
if(CommandEvents != null)
{
CommandEvents.AfterExecute -= OnAfterExecute;
CommandEvents.BeforeExecute -= OnBeforeExecute;
}
this.isDisposed = true;
}
}
private void OnAfterExecute(string Guid, int ID, object CustomIn, object CustomOut)
{
if(AfterExecute != null)
{
AfterExecute(this, new EventArgs());
}
}
private void OnBeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
if(BeforeExecute != null)
{
BeforeExecute(this, new EventArgs());
}
}
}
Usage
VSCommandInterceptor interceptor =
new VSCommandInterceptor(
serviceProvider,
typeof(Microsoft.VisualStudio.VSConstants.VSStd97CmdID).GUID,
(int)Microsoft.VisualStudio.VSConstants.VSStd97CmdID.CleanSln);
interceptor.BeforeExecute += new EventHandler<EventArgs>(BeforeExecute);
private void BeforeExecute(object sender, EventArgs e)
{
//TODO: Provide logic
}
Assemblies needed
Stay tuned,
Pablo