/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/VisualStudio/Core/Def/Utilities/VisualStudioCommandHandlerHelpers.cs
67 строк
2 KB
Jason Malinowski
Avoid switching to the UI thread every time we are subscribing menus
12 мар 2026, 21:06
12 мар 2026, 21:06
64b0b7c
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; namespace Microsoft.VisualStudio.LanguageServices.Implementation.Utilities; internal static class VisualStudioCommandHandlerHelpers { public static bool TryGetSelectedProjectHierarchy(IServiceProvider? serviceProvider, [NotNullWhen(returnValue: true)] out IVsHierarchy? hierarchy) { hierarchy = null; // Get the DTE service and make sure there is an open solution if (serviceProvider?.GetService(typeof(EnvDTE.DTE)) is not EnvDTE.DTE dte || dte.Solution == null) { return false; } var selectionHierarchy = IntPtr.Zero; var selectionContainer = IntPtr.Zero; // Get the current selection in the shell if (serviceProvider.GetService(typeof(SVsShellMonitorSelection)) is IVsMonitorSelection monitorSelection) { try { monitorSelection.GetCurrentSelection(out selectionHierarchy, out var itemId, out var multiSelect, out selectionContainer); if (selectionHierarchy != IntPtr.Zero) { hierarchy = Marshal.GetObjectForIUnknown(selectionHierarchy) as IVsHierarchy; Debug.Assert(hierarchy != null); return hierarchy != null; } } catch (Exception) { // If anything went wrong, just ignore it } finally { // Make sure we release the COM pointers in any case if (selectionHierarchy != IntPtr.Zero) { Marshal.Release(selectionHierarchy); } if (selectionContainer != IntPtr.Zero) { Marshal.Release(selectionContainer); } } } return false; } public static bool IsBuildActive() => KnownUIContexts.SolutionBuildingContext.IsActive; }