Skip to content

Instantly share code, notes, and snippets.

@yuyoyuppe
Created December 10, 2019 12:06
Show Gist options
  • Save yuyoyuppe/e7e83d197827f51682b51ea9dd79a42a to your computer and use it in GitHub Desktop.
Save yuyoyuppe/e7e83d197827f51682b51ea9dd79a42a to your computer and use it in GitHub Desktop.
Visual Studio Extension to enable binding reload project action to a hotkey
//------------------------------------------------------------------------------
// <copyright file="ReloadProject.cs" company="Company">
// Copyright (c) Company. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.ComponentModel.Design;
using System.Globalization;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using EnvDTE80;
using EnvDTE;
/*
*
*
*
*
*
*
*
* https://msdn.microsoft.com/en-us/library/dd885242.aspx
https://msdn.microsoft.com/en-us/library/ee361745.aspx
https://msdn.microsoft.com/en-us/library/dd885121.aspx
https://msdn.microsoft.com/en-us/library/ee197665.aspx
https://msdn.microsoft.com/en-us/library/ee197650.aspx
*
*
*
*
*
*/
namespace ReloadProjectExtension
{
/// <summary>
/// Command handler
/// </summary>
///
internal sealed class ReloadProjectCmd
{
/// <summary>
/// Command ID.
/// </summary>
public
const int CommandId = 0x0100;
/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public
static readonly Guid CommandSet = new Guid("fc6b0341-ab07-4dfd-8c63-d00a914c216a");
/// <summary>
/// VS Package that provides this command, not null.
/// </summary>
private
readonly Package package;
/// <summary>
/// Initializes a new instance of the <see cref="ReloadProject"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
/// </summary>
/// <param name="package">Owner package, not null.</param>
private
ReloadProjectCmd(Package package)
{
if (package == null)
{
throw new ArgumentNullException("package");
}
this.package = package;
OleMenuCommandService commandService = this.ServiceProvider.GetService(
typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
var menuCommandID = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
commandService.AddCommand(menuItem);
}
}
/// <summary>
/// Gets the instance of the command.
/// </summary>
public
static ReloadProjectCmd Instance
{
get;
private
set;
}
/// <summary>
/// Gets the service provider from the owner package.
/// </summary>
private
IServiceProvider ServiceProvider
{
get { return this.package; }
}
/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>
/// <param name="package">Owner package, not null.</param>
public
static void Initialize(Package package)
{ Instance = new ReloadProjectCmd(package); }
/// <summary>
/// This function is the callback used to execute the command when the menu item is clicked.
/// See the constructor to see how the menu item is associated with this function using
/// OleMenuCommandService service and MenuCommand class.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event args.</param>
private
void MenuItemCallback(object sender, EventArgs e)
{
var dte = Package.GetGlobalService(typeof(SDTE)) as DTE2;
string message = "";
var fn = dte.Solution.FullName;
dte.Solution.Close();
while (dte.Solution.IsOpen);
dte.Solution.Open(fn);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment