public void Constructor_InvokeCallsDerivedMethod() { // Arrange var spMock = Mock.Of <IServiceProvider>(); var cs = new OleMenuCommandService(spMock); var casMock = Mock.Of <ICommandAvailabilityService>(); const int commandIdInt = 4711; var commandSet = Guid.Parse("{110031CC-14A1-44FA-83D1-D970918981AC}"); var commandId = new CommandID(commandSet, commandIdInt); object executeSender = null; EventArgs executeArgs = null; // Act var cmd = new BaseCommandTestImplementation(cs, casMock, commandIdInt, commandSet) { ExecuteCalledCallback = (o, args) => { executeSender = o; executeArgs = args; } }; // Assert Assert.IsNotNull(cmd); var invoked = cs.GlobalInvoke(commandId); Assert.IsTrue(invoked); Assert.IsNotNull(executeSender); Assert.IsNotNull(executeArgs); }
private void Execute(object sender, EventArgs e) { Guid cmdGroup = typeof(VSConstants.VSStd97CmdID).GUID; var cmd = new CommandID(cmdGroup, VSConstants.cmdidToolsOptions); _commandService.GlobalInvoke(cmd, typeof(FormatterOptions).GUID.ToString()); }
internal static void GlobalInvoke(this IServiceProvider serviceProvider, CommandID cmdID, object arg) { OleMenuCommandService mcs = serviceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService; mcs.GlobalInvoke(cmdID, arg); }
public static void SetSqlEditorConnection(IVsTextLines ppBuffer, ISqlContextDeterminator contextDeterminator, OleMenuCommandService commandService, CommandID sqlEditorConnectCmdId) { if (ppBuffer != null && contextDeterminator != null && _sqlExtractionFile != null) { #if DEV15_OR_LATER Assembly dll = Assembly.Load(@"Microsoft.VisualStudio.Data.Tools.SqlEditor, Version=15.0.0.0"); #elif DEV14_OR_LATER Assembly dll = Assembly.Load(@"Microsoft.VisualStudio.Data.Tools.SqlEditor, Version=14.0.0.0"); #else Assembly dll = Assembly.Load(@"Microsoft.VisualStudio.Data.Tools.SqlEditor, Version=12.0.0.0"); #endif if (dll != null) { Type sqlEditorPackageType = dll.GetType("Microsoft.VisualStudio.Data.Tools.SqlEditor.VSIntegration.SqlEditorPackage"); if (sqlEditorPackageType != null) { // get the GetAuxiliaryDocData method var instanceProp = sqlEditorPackageType.GetProperty("Instance", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); if (instanceProp != null) { var instanceVal = instanceProp.GetValue(null, null); if (instanceVal != null) { var getAuxMethod = sqlEditorPackageType.GetMethod("GetAuxillaryDocData", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy); if (getAuxMethod != null) { var auxiliaryDocDataVal = getAuxMethod.Invoke(instanceVal, new object[] { ppBuffer }); if (auxiliaryDocDataVal != null) { // set the sql connection (IDbConnection) on the QueryExecutor.ConnectionStrategy var queryExecutorType = auxiliaryDocDataVal.GetType().GetProperty("QueryExecutor", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy); if (queryExecutorType != null) { var queryExecutorVal = queryExecutorType.GetValue(auxiliaryDocDataVal, null); if (queryExecutorVal != null) { var connectionStrategyType = queryExecutorVal.GetType().GetProperty("ConnectionStrategy", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy); if (connectionStrategyType != null) { var connectionStrategyVal = connectionStrategyType.GetValue(queryExecutorVal, null); if (connectionStrategyVal != null) { // set the _connection field var connectionField = connectionStrategyVal.GetType().GetAllFields().Where(x => x.Name == "_connection").FirstOrDefault(); var uiConnectionField = connectionStrategyVal.GetType().GetAllFields().Where(x => x.Name == "_connectionInfo").FirstOrDefault(); if (connectionField != null && uiConnectionField != null) { string server, database; if (contextDeterminator.DetermineSqlContext(_sqlExtractionFile, out server, out database)) { SqlConnection sqlCon = new SqlConnection(string.Format(@"Data Source={0};Integrated Security=true;", server)); sqlCon.Open(); sqlCon.ChangeDatabase(database); connectionField.SetValue(connectionStrategyVal, sqlCon); try { UIConnectionInfo connInfo = new UIConnectionInfo() { ServerName = server, UserName = string.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName) }; uiConnectionField.SetValue(connectionStrategyVal, connInfo); } catch (ArgumentException ae) { // In VS2015 this exception gets thrown, but everything works correctly. // So for the time being, we'll just keep going } if (commandService != null) { commandService.GlobalInvoke(sqlEditorConnectCmdId); } // attempt to set the QueryExecutor's IsConnected value var isConnProp = queryExecutorVal.GetType().GetProperty("IsConnected", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy); if (isConnProp != null) { isConnProp.SetValue(queryExecutorVal, true, null); } } } } } } } } } } } } } } }
internal void GlobalInvoke(CommandID cmdID, object arg) { OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; mcs.GlobalInvoke(cmdID, arg); }