Pablo Galiano : How do I intercept a Visual Studio command execution

Subscriptions

<September 2010>
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

News

Subscribe to Pablo Galiano by Email

Post Categories

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

  • EnvDTE

 

Stay tuned,

Pablo

posted on Tuesday, August 19, 2008 8:31 AM by pga

# Extensibility How to #3 and #4 @ Wednesday, August 20, 2008 1:43 AM

Pablo continues his series on various how to scenarios in VS Extensibility - How do I intercept a Visual

Anonymous

# re: How do I intercept a Visual Studio command execution @ Thursday, October 23, 2008 1:48 AM

jB7l8S <a href="http://ewecduhhhkdx.com/">ewecduhhhkdx</a>, [url=http://ycznqtujcnqe.com/]ycznqtujcnqe[/url], [link=http://uoyowwddjtwy.com/]uoyowwddjtwy[/link], http://lhulkzaagtlt.com/

ogcehqnestz

# re: How do I intercept a Visual Studio command execution @ Thursday, October 23, 2008 1:49 AM

jB7l8S <a href="http://ewecduhhhkdx.com/">ewecduhhhkdx</a>, [url=http://ycznqtujcnqe.com/]ycznqtujcnqe[/url], [link=http://uoyowwddjtwy.com/]uoyowwddjtwy[/link], http://lhulkzaagtlt.com/

ogcehqnestz