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
- EnvDTE
Â
Stay tuned,
Pablo


Hello Pablo,
I came across this example which does exactly what I need to do. However, it makes use of classes that Microsofot document as ‘infrastructure’ and that are intended for their own internal use.
Is there no other way to intercept or react to commands?