Click here to Skip to main content
15,895,746 members
Articles / Containers / Virtual Machine

A simple Switch Framework for the Visual Studio prompt and the devenv command

Rate me:
Please Sign up or sign in to vote.
4.78/5 (11 votes)
20 Apr 2007CPOL3 min read 53.1K   313   13  
A library for creating custom switches for the devenv command prompt (extensibility example).
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Xml;
using EnvDTE80;
using Microsoft.VisualStudio.Shell.Interop;

namespace SwitchFramework
{
    /// <summary>
    /// Base abstract class for all switches.
    /// </summary>
    internal abstract class CustomSwitch
    {
        #region Events and Delegates

        /// <summary>
        /// Fires before executing switch.
        /// </summary>
        virtual public event EventHandler BeforeExecuting;

        /// <summary>
        /// Fires after executing switch.
        /// </summary>
        virtual public event EventHandler AfterExecuting;

        #endregion

        #region Constructors

        /// <summary>
        /// Default contructor.
        /// </summary>
        protected CustomSwitch()
        {
        }

        /// <summary>
        /// Contructor.
        /// </summary>
        /// <param name="package">Package that launch command.</param>
        /// <param name="name">Custom switch name.</param>
        /// <param name="parameters">Custom switch parameters.</param>
        protected CustomSwitch(ISwitchPackage package, string name, string[] parameters)
        {
            _package = package;
            _name = name;
            _parameters = parameters;
        }

        #endregion

        #region Properties

        ISwitchPackage _package = null;
        /// <summary>
        /// Gets ISwitchPackage.
        /// </summary>
        protected ISwitchPackage Package
        {
            get { return _package; }
        }

        string _name = string.Empty;
        /// <summary>
        /// Gets switch name.
        /// </summary>
        public string Name
        {
            get { return _name; }
        }

        string[] _parameters = null;
        /// <summary>
        /// Gets switch parameters.
        /// </summary>
        public string[] Parameters
        {
            get { return _parameters; }
        }

        #endregion

        #region Public Functions

        /// <summary>
        /// Checks parameters and executes the command.
        /// </summary>
        virtual public void Execute()
        {
            if (!CheckContext())
                throw new SwitchException(Resources.IllegalSwitchOrOption);

            if (BeforeExecuting != null)
                BeforeExecuting(this, null);

            Executing();

            if (AfterExecuting != null)
                AfterExecuting(this, null);
        }

        #endregion

        #region Protected Functions

        /// <summary>
        /// Executes the command.
        /// </summary>
        abstract protected void Executing();

        /// <summary>
        /// Checks switch and options.
        /// </summary>
        /// <returns>Returns true if all right.</returns>
        virtual protected bool CheckContext()
        {
            return true;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Italy Italy
I am a biomedical engineer. I work in Genoa as software developer. I developed MFC ActiveX controls for industrial automation for 2 years and packages for Visual Studio 2005 for 1 year. Currently I'm working in .NET 3.5 in biomedical area.

Comments and Discussions