From 96b6832dff23efdbdcb0e32b06f940025c1afcda Mon Sep 17 00:00:00 2001 From: bao-qian Date: Mon, 2 Nov 2015 13:42:52 +0000 Subject: [PATCH] Remove LoadInterfacesFromAppDomain Much faster. ApplyPluginLanguages is 10 times faster now. 7ms vs 100ms. --- Wox.Core/AssemblyHelper.cs | 9 ------- Wox.Core/Theme/Theme.cs | 2 +- Wox.Core/UI/ResourceMerger.cs | 35 +++++++++------------------ Wox.Core/i18n/Internationalization.cs | 2 +- 4 files changed, 13 insertions(+), 35 deletions(-) diff --git a/Wox.Core/AssemblyHelper.cs b/Wox.Core/AssemblyHelper.cs index dd47eb73b5..ec1e27218b 100644 --- a/Wox.Core/AssemblyHelper.cs +++ b/Wox.Core/AssemblyHelper.cs @@ -23,14 +23,5 @@ namespace Wox.Core } return results; } - - public static List LoadInterfacesFromAppDomain() where T : class - { - var interfaceObjects = AppDomain.CurrentDomain.GetAssemblies() - .SelectMany(s => s.GetTypes()) - .Where(p => p.IsClass && !p.IsAbstract && p.GetInterfaces().Contains(typeof(T))); - - return interfaceObjects.Select(interfaceObject => (T) Activator.CreateInstance(interfaceObject)).ToList(); - } } } diff --git a/Wox.Core/Theme/Theme.cs b/Wox.Core/Theme/Theme.cs index 4fba143bcd..114b5742c7 100644 --- a/Wox.Core/Theme/Theme.cs +++ b/Wox.Core/Theme/Theme.cs @@ -56,7 +56,7 @@ namespace Wox.Core.Theme UserSettingStorage.Instance.Theme = themeName; UserSettingStorage.Instance.Save(); - ResourceMerger.ApplyThemeResource(); + ResourceMerger.ApplyThemeResource(this); } public ResourceDictionary GetResourceDictionary() diff --git a/Wox.Core/UI/ResourceMerger.cs b/Wox.Core/UI/ResourceMerger.cs index 0d1539d896..c9a22493f5 100644 --- a/Wox.Core/UI/ResourceMerger.cs +++ b/Wox.Core/UI/ResourceMerger.cs @@ -24,44 +24,31 @@ namespace Wox.Core.UI } } - public static void ApplyThemeResource() + public static void ApplyThemeResource(Theme.Theme t) { RemoveResource(Theme.Theme.DirectoryName); - ApplyUIResources(); + Application.Current.Resources.MergedDictionaries.Add(t.GetResourceDictionary()); } - public static void ApplyLanguageResources() + public static void ApplyLanguageResources(Internationalization i) { RemoveResource(Internationalization.DirectoryName); - ApplyUIResources(); - } - - private static void ApplyUIResources() - { - var UIResources = AssemblyHelper.LoadInterfacesFromAppDomain(); - foreach (var uiResource in UIResources) - { - Application.Current.Resources.MergedDictionaries.Add(uiResource.GetResourceDictionary()); - } + Application.Current.Resources.MergedDictionaries.Add(i.GetResourceDictionary()); } internal static void ApplyPluginLanguages() { RemoveResource(PluginManager.DirectoryName); - var pluginI18ns = AssemblyHelper.LoadInterfacesFromAppDomain(); - foreach (var pluginI18n in pluginI18ns) + foreach (var languageFile in (PluginManager.AllPlugins.Select(p => p.Plugin). + Where(plugin => plugin.GetType().GetInterfaces().Contains(typeof(IPluginI18n))). + Select(plugin => InternationalizationManager.Instance.GetLanguageFile(((IPluginI18n)plugin).GetLanguagesFolder())). + Where(file => !string.IsNullOrEmpty(file)))) { - string languageFile = InternationalizationManager.Instance.GetLanguageFile(pluginI18n.GetLanguagesFolder()); - if (!string.IsNullOrEmpty(languageFile)) + Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { - Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary - { - Source = new Uri(languageFile, UriKind.Absolute) - }); - } + Source = new Uri(languageFile, UriKind.Absolute) + }); } } - - } } \ No newline at end of file diff --git a/Wox.Core/i18n/Internationalization.cs b/Wox.Core/i18n/Internationalization.cs index 3f8e4ecbea..cc0d80e837 100644 --- a/Wox.Core/i18n/Internationalization.cs +++ b/Wox.Core/i18n/Internationalization.cs @@ -70,7 +70,7 @@ namespace Wox.Core.i18n UserSettingStorage.Instance.Language = language.LanguageCode; UserSettingStorage.Instance.Save(); - ResourceMerger.ApplyLanguageResources(); + ResourceMerger.ApplyLanguageResources(this); } public ResourceDictionary GetResourceDictionary()