From 20bbdbcb0ecba73bb9a6064fc826b1176caba109 Mon Sep 17 00:00:00 2001 From: reduckted Date: Wed, 1 Nov 2023 20:36:15 +1000 Subject: [PATCH] Change RegisterToolWindows to be ToolkitPackage extension method. --- .../AsyncPackageExtensions.cs | 29 ------------- .../ToolkitPackageExtensions.cs | 43 +++++++++++++++++++ .../VSSDK.Helpers.Shared.projitems | 1 + 3 files changed, 44 insertions(+), 29 deletions(-) create mode 100644 src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/ToolkitPackageExtensions.cs diff --git a/src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/AsyncPackageExtensions.cs b/src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/AsyncPackageExtensions.cs index b658a30..4726c05 100644 --- a/src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/AsyncPackageExtensions.cs +++ b/src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/AsyncPackageExtensions.cs @@ -47,34 +47,5 @@ public static async Task> RegisterCommandsAsync(this AsyncPa } return commands; } - - /// - /// Automatically calls the method for every BaseToolWindow<> in the package or provided assemblies. - /// - /// - /// - /// - public static void RegisterToolWindows(this AsyncPackage package, params Assembly[] assemblies) - { - List assembliesList = assemblies.ToList(); - Assembly packageAssembly = package.GetType().Assembly; - if (!assembliesList.Contains(packageAssembly)) - assembliesList.Add(packageAssembly); - - Type baseToolWindowType = typeof(BaseToolWindow<>); - IEnumerable toolWindowTypes = assembliesList.SelectMany(x => x.GetTypes()) - .Where(x => - !x.IsAbstract - && x.IsAssignableToGenericType(baseToolWindowType)); - - foreach (Type? toolWindowtype in toolWindowTypes) - { - MethodInfo initializeMethod = toolWindowtype.GetMethod( - "Initialize", - BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); - - initializeMethod.Invoke(null, new object[] { package }); - } - } } } diff --git a/src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/ToolkitPackageExtensions.cs b/src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/ToolkitPackageExtensions.cs new file mode 100644 index 0000000..c5aefa6 --- /dev/null +++ b/src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/ToolkitPackageExtensions.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Microsoft.VisualStudio.Shell; + +namespace Community.VisualStudio.Toolkit +{ + /// + /// Extensions for an + /// + public static class ToolkitPackageExtensions + { + /// + /// Automatically calls the + /// method for every in the package or provided assemblies. + /// + /// The package that contains the tool windows to register. + /// The additional assemblies to look for tool windows in. + public static void RegisterToolWindows(this ToolkitPackage package, params Assembly[] assemblies) + { + List assembliesList = assemblies.ToList(); + Assembly packageAssembly = package.GetType().Assembly; + if (!assembliesList.Contains(packageAssembly)) + assembliesList.Add(packageAssembly); + + Type baseToolWindowType = typeof(BaseToolWindow<>); + IEnumerable toolWindowTypes = assembliesList.SelectMany(x => x.GetTypes()) + .Where(x => + !x.IsAbstract + && x.IsAssignableToGenericType(baseToolWindowType)); + + foreach (Type? toolWindowtype in toolWindowTypes) + { + MethodInfo initializeMethod = toolWindowtype.GetMethod( + "Initialize", + BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); + + initializeMethod.Invoke(null, new object[] { package }); + } + } + } +} diff --git a/src/toolkit/Community.VisualStudio.Toolkit.Shared/VSSDK.Helpers.Shared.projitems b/src/toolkit/Community.VisualStudio.Toolkit.Shared/VSSDK.Helpers.Shared.projitems index e5c6c98..b1eaf39 100644 --- a/src/toolkit/Community.VisualStudio.Toolkit.Shared/VSSDK.Helpers.Shared.projitems +++ b/src/toolkit/Community.VisualStudio.Toolkit.Shared/VSSDK.Helpers.Shared.projitems @@ -43,6 +43,7 @@ +