From 39e879377ecf4802886a8c43dd99dbe2b6e3e917 Mon Sep 17 00:00:00 2001 From: Andreas Weizel Date: Sat, 28 Sep 2019 13:34:01 +0200 Subject: [PATCH] Fix VSIX analyzer warnings and disabled the analyzer --- ILSpy.AddIn/Commands/NuGetReferenceForILSpy.cs | 5 +++++ ILSpy.AddIn/Commands/OpenCodeItemCommand.cs | 9 +++++++++ ILSpy.AddIn/Commands/OpenILSpyCommand.cs | 6 ++++++ ILSpy.AddIn/Commands/OpenProjectOutputCommand.cs | 4 ++++ ILSpy.AddIn/Commands/OpenReferenceCommand.cs | 4 ++++ ILSpy.AddIn/Commands/ProjectItemForILSpy.cs | 5 +++++ ILSpy.AddIn/Commands/ProjectReferenceForILSpy.cs | 5 +++++ ILSpy.AddIn/ILSpy.AddIn.csproj | 1 - ILSpy.AddIn/ILSpyAddInPackage.cs | 6 ++++++ ILSpy.AddIn/ILSpyInstance.cs | 1 + ILSpy.AddIn/Utils.cs | 11 +++++++++++ 11 files changed, 56 insertions(+), 1 deletion(-) diff --git a/ILSpy.AddIn/Commands/NuGetReferenceForILSpy.cs b/ILSpy.AddIn/Commands/NuGetReferenceForILSpy.cs index bcbaa82a3..84ee74af5 100644 --- a/ILSpy.AddIn/Commands/NuGetReferenceForILSpy.cs +++ b/ILSpy.AddIn/Commands/NuGetReferenceForILSpy.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using EnvDTE; +using Microsoft.VisualStudio.Shell; namespace ICSharpCode.ILSpy.AddIn.Commands { @@ -26,6 +27,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands /// instance or null, if item is not a supported project. public static NuGetReferenceForILSpy Detect(object itemData) { + ThreadHelper.ThrowIfNotOnUIThread(); + if (itemData is ProjectItem projectItem) { var properties = Utils.GetProperties(projectItem.Properties, "Type"); if ((properties[0] as string) == "Package") { @@ -42,6 +45,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands /// Parameters object or null, if not applicable. public ILSpyParameters GetILSpyParameters() { + ThreadHelper.ThrowIfNotOnUIThread(); + var properties = Utils.GetProperties(projectItem.Properties, "Name", "Version", "Path"); if (properties[0] != null && properties[1] != null && properties[2] != null) { return new ILSpyParameters(new[] { $"{properties[2]}\\{properties[0]}.{properties[1]}.nupkg" }); diff --git a/ILSpy.AddIn/Commands/OpenCodeItemCommand.cs b/ILSpy.AddIn/Commands/OpenCodeItemCommand.cs index 0f26fa2d6..17476d109 100644 --- a/ILSpy.AddIn/Commands/OpenCodeItemCommand.cs +++ b/ILSpy.AddIn/Commands/OpenCodeItemCommand.cs @@ -23,6 +23,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands protected override void OnBeforeQueryStatus(object sender, EventArgs e) { + ThreadHelper.ThrowIfNotOnUIThread(); + if (sender is OleMenuCommand menuItem) { menuItem.Visible = false; @@ -46,6 +48,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands Document GetRoslynDocument() { + ThreadHelper.ThrowIfNotOnUIThread(); + var document = owner.DTE.ActiveDocument; var selection = (EnvDTE.TextPoint)((EnvDTE.TextSelection)document.Selection).ActivePoint; var id = owner.Workspace.CurrentSolution.GetDocumentIdsWithFilePath(document.FullName).FirstOrDefault(); @@ -57,12 +61,16 @@ namespace ICSharpCode.ILSpy.AddIn.Commands EnvDTE.TextPoint GetEditorSelection() { + ThreadHelper.ThrowIfNotOnUIThread(); + var document = owner.DTE.ActiveDocument; return ((EnvDTE.TextSelection)document.Selection).ActivePoint; } protected override async void OnExecute(object sender, EventArgs e) { + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + var textView = Utils.GetCurrentViewHost(owner)?.TextView; if (textView == null) return; @@ -89,6 +97,7 @@ namespace ICSharpCode.ILSpy.AddIn.Commands // Add our own project as well (not among references) var project = FindProject(owner.DTE.Solution.Projects.OfType(), roslynProject.FilePath); + if (project == null) { owner.ShowMessage(OLEMSGICON.OLEMSGICON_WARNING, "Can't show ILSpy for this code element!"); return; diff --git a/ILSpy.AddIn/Commands/OpenILSpyCommand.cs b/ILSpy.AddIn/Commands/OpenILSpyCommand.cs index 35a2cc29f..acb7b12b9 100644 --- a/ILSpy.AddIn/Commands/OpenILSpyCommand.cs +++ b/ILSpy.AddIn/Commands/OpenILSpyCommand.cs @@ -55,6 +55,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands protected void OpenAssembliesInILSpy(ILSpyParameters parameters) { + ThreadHelper.ThrowIfNotOnUIThread(); + if (parameters == null) return; @@ -71,6 +73,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands protected Dictionary GetReferences(Microsoft.CodeAnalysis.Project parentProject) { + ThreadHelper.ThrowIfNotOnUIThread(); + var dict = new Dictionary(); foreach (var reference in parentProject.MetadataReferences) { using (var assemblyDef = AssemblyDefinition.ReadAssembly(reference.Display)) { @@ -97,6 +101,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands protected EnvDTE.Project FindProject(IEnumerable projects, string projectFile) { + ThreadHelper.ThrowIfNotOnUIThread(); + foreach (var project in projects) { switch (project.Kind) { case DTEConstants.vsProjectKindSolutionItems: diff --git a/ILSpy.AddIn/Commands/OpenProjectOutputCommand.cs b/ILSpy.AddIn/Commands/OpenProjectOutputCommand.cs index 8fd06a50c..937d1601a 100644 --- a/ILSpy.AddIn/Commands/OpenProjectOutputCommand.cs +++ b/ILSpy.AddIn/Commands/OpenProjectOutputCommand.cs @@ -17,6 +17,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands protected override void OnBeforeQueryStatus(object sender, EventArgs e) { + ThreadHelper.ThrowIfNotOnUIThread(); + if (sender is OleMenuCommand menuItem) { menuItem.Visible = false; @@ -27,6 +29,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands protected override void OnExecute(object sender, EventArgs e) { + ThreadHelper.ThrowIfNotOnUIThread(); + if (owner.DTE.SelectedItems.Count != 1) return; var projectItemWrapper = ProjectItemForILSpy.Detect(owner, owner.DTE.SelectedItems.Item(1)); diff --git a/ILSpy.AddIn/Commands/OpenReferenceCommand.cs b/ILSpy.AddIn/Commands/OpenReferenceCommand.cs index dd6ab3581..e97a2af35 100644 --- a/ILSpy.AddIn/Commands/OpenReferenceCommand.cs +++ b/ILSpy.AddIn/Commands/OpenReferenceCommand.cs @@ -22,6 +22,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands protected override void OnBeforeQueryStatus(object sender, EventArgs e) { + ThreadHelper.ThrowIfNotOnUIThread(); + if (sender is OleMenuCommand menuItem) { menuItem.Visible = false; @@ -44,6 +46,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands protected override void OnExecute(object sender, EventArgs e) { + ThreadHelper.ThrowIfNotOnUIThread(); + var itemObject = owner.GetSelectedItemsData().FirstOrDefault(); if (itemObject == null) return; diff --git a/ILSpy.AddIn/Commands/ProjectItemForILSpy.cs b/ILSpy.AddIn/Commands/ProjectItemForILSpy.cs index 9cc73346a..182565b35 100644 --- a/ILSpy.AddIn/Commands/ProjectItemForILSpy.cs +++ b/ILSpy.AddIn/Commands/ProjectItemForILSpy.cs @@ -1,6 +1,7 @@ using System.IO; using System.Linq; using EnvDTE; +using Microsoft.VisualStudio.Shell; namespace ICSharpCode.ILSpy.AddIn.Commands { @@ -27,6 +28,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands /// instance or null, if item is not a supported project. public static ProjectItemForILSpy Detect(ILSpyAddInPackage package, SelectedItem item) { + ThreadHelper.ThrowIfNotOnUIThread(); + var project = item.Project; var roslynProject = package.Workspace.CurrentSolution.Projects.FirstOrDefault(p => p.FilePath == project.FileName); if (roslynProject == null) @@ -42,6 +45,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands /// Parameters object or null, if not applicable. public ILSpyParameters GetILSpyParameters(ILSpyAddInPackage package) { + ThreadHelper.ThrowIfNotOnUIThread(); + return new ILSpyParameters(new[] { Utils.GetProjectOutputAssembly(project, roslynProject) }); } } diff --git a/ILSpy.AddIn/Commands/ProjectReferenceForILSpy.cs b/ILSpy.AddIn/Commands/ProjectReferenceForILSpy.cs index 743f29292..57f13d8a8 100644 --- a/ILSpy.AddIn/Commands/ProjectReferenceForILSpy.cs +++ b/ILSpy.AddIn/Commands/ProjectReferenceForILSpy.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using EnvDTE; +using Microsoft.VisualStudio.Shell; using Mono.Cecil; namespace ICSharpCode.ILSpy.AddIn.Commands @@ -31,6 +32,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands /// instance or null, if item is not a supported project. public static ProjectReferenceForILSpy Detect(object itemData) { + ThreadHelper.ThrowIfNotOnUIThread(); + if (itemData is ProjectItem projectItem) { var properties = Utils.GetProperties(projectItem.Properties, "FusionName", "ResolvedPath"); string fusionName = properties[0] as string; @@ -50,6 +53,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands /// Parameters object or null, if not applicable. public ILSpyParameters GetILSpyParameters(Dictionary projectReferences) { + ThreadHelper.ThrowIfNotOnUIThread(); + string fileName = projectItem.ContainingProject?.FileName; if (!string.IsNullOrEmpty(fileName)) { if (projectReferences.TryGetValue(projectItem.Name, out DetectedReference path)) { diff --git a/ILSpy.AddIn/ILSpy.AddIn.csproj b/ILSpy.AddIn/ILSpy.AddIn.csproj index 1126fcf82..e61b4e2a5 100644 --- a/ILSpy.AddIn/ILSpy.AddIn.csproj +++ b/ILSpy.AddIn/ILSpy.AddIn.csproj @@ -51,7 +51,6 @@ - diff --git a/ILSpy.AddIn/ILSpyAddInPackage.cs b/ILSpy.AddIn/ILSpyAddInPackage.cs index aedbda510..533a78bdd 100644 --- a/ILSpy.AddIn/ILSpyAddInPackage.cs +++ b/ILSpy.AddIn/ILSpyAddInPackage.cs @@ -113,6 +113,10 @@ namespace ICSharpCode.ILSpy.AddIn ThreadHelper.ThrowIfNotOnUIThread(); IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell)); + if (uiShell == null) { + return 0; + } + Guid clsid = Guid.Empty; int result; Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure( @@ -136,6 +140,8 @@ namespace ICSharpCode.ILSpy.AddIn public IEnumerable GetSelectedItemsData() { + ThreadHelper.ThrowIfNotOnUIThread(); + if (DTE.ToolWindows.SolutionExplorer.SelectedItems is IEnumerable hierarchyItems) { foreach (var item in hierarchyItems) { if (item.Object is T typedItem) { diff --git a/ILSpy.AddIn/ILSpyInstance.cs b/ILSpy.AddIn/ILSpyInstance.cs index bd8c56cb7..adf922328 100644 --- a/ILSpy.AddIn/ILSpyInstance.cs +++ b/ILSpy.AddIn/ILSpyInstance.cs @@ -54,6 +54,7 @@ namespace ICSharpCode.ILSpy.AddIn } } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD110:Observe result of async calls", Justification = "")] void SendMessage(Process ilspyProcess, string message, bool activate) { // We wait asynchronously until target window can be found and try to find it multiple times diff --git a/ILSpy.AddIn/Utils.cs b/ILSpy.AddIn/Utils.cs index 20fa53e0b..74af2d797 100644 --- a/ILSpy.AddIn/Utils.cs +++ b/ILSpy.AddIn/Utils.cs @@ -8,6 +8,7 @@ using System.Text; using EnvDTE; using Microsoft.VisualStudio.Editor; using Microsoft.VisualStudio.LanguageServices; +using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.TextManager.Interop; @@ -148,6 +149,8 @@ namespace ICSharpCode.ILSpy.AddIn public static object[] GetProperties(EnvDTE.Properties properties, params string[] names) { + ThreadHelper.ThrowIfNotOnUIThread(); + var values = new object[names.Length]; foreach (object p in properties) { try { @@ -168,6 +171,8 @@ namespace ICSharpCode.ILSpy.AddIn public static List<(string, object)> GetAllProperties(EnvDTE.Properties properties) { + ThreadHelper.ThrowIfNotOnUIThread(); + var result = new List<(string, object)>(); for (int i = 0; i < properties.Count; i++) { try { @@ -207,6 +212,10 @@ namespace ICSharpCode.ILSpy.AddIn public static IWpfTextViewHost GetCurrentViewHost(IServiceProvider serviceProvider) { IVsTextManager txtMgr = (IVsTextManager)serviceProvider.GetService(typeof(SVsTextManager)); + if (txtMgr == null) { + return null; + } + IVsTextView vTextView = null; int mustHaveFocus = 1; txtMgr.GetActiveView(mustHaveFocus, null, out vTextView); @@ -235,6 +244,8 @@ namespace ICSharpCode.ILSpy.AddIn public static string GetProjectOutputAssembly(Project project, Microsoft.CodeAnalysis.Project roslynProject) { + ThreadHelper.ThrowIfNotOnUIThread(); + string outputFileName = Path.GetFileName(roslynProject.OutputFilePath); // Get the directory path based on the project file.