Remove LoadInterfacesFromAppDomain

Much faster. ApplyPluginLanguages is 10 times faster now. 7ms vs 100ms.
This commit is contained in:
bao-qian 2015-11-02 13:42:52 +00:00
parent 881e414043
commit 96b6832dff
4 changed files with 13 additions and 35 deletions

View File

@ -23,14 +23,5 @@ namespace Wox.Core
} }
return results; return results;
} }
public static List<T> LoadInterfacesFromAppDomain<T>() 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();
}
} }
} }

View File

@ -56,7 +56,7 @@ namespace Wox.Core.Theme
UserSettingStorage.Instance.Theme = themeName; UserSettingStorage.Instance.Theme = themeName;
UserSettingStorage.Instance.Save(); UserSettingStorage.Instance.Save();
ResourceMerger.ApplyThemeResource(); ResourceMerger.ApplyThemeResource(this);
} }
public ResourceDictionary GetResourceDictionary() public ResourceDictionary GetResourceDictionary()

View File

@ -24,44 +24,31 @@ namespace Wox.Core.UI
} }
} }
public static void ApplyThemeResource() public static void ApplyThemeResource(Theme.Theme t)
{ {
RemoveResource(Theme.Theme.DirectoryName); 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); RemoveResource(Internationalization.DirectoryName);
ApplyUIResources(); Application.Current.Resources.MergedDictionaries.Add(i.GetResourceDictionary());
}
private static void ApplyUIResources()
{
var UIResources = AssemblyHelper.LoadInterfacesFromAppDomain<IUIResource>();
foreach (var uiResource in UIResources)
{
Application.Current.Resources.MergedDictionaries.Add(uiResource.GetResourceDictionary());
}
} }
internal static void ApplyPluginLanguages() internal static void ApplyPluginLanguages()
{ {
RemoveResource(PluginManager.DirectoryName); RemoveResource(PluginManager.DirectoryName);
var pluginI18ns = AssemblyHelper.LoadInterfacesFromAppDomain<IPluginI18n>(); foreach (var languageFile in (PluginManager.AllPlugins.Select(p => p.Plugin).
foreach (var pluginI18n in pluginI18ns) 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()); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
if (!string.IsNullOrEmpty(languageFile))
{ {
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary Source = new Uri(languageFile, UriKind.Absolute)
{ });
Source = new Uri(languageFile, UriKind.Absolute)
});
}
} }
} }
} }
} }

View File

@ -70,7 +70,7 @@ namespace Wox.Core.i18n
UserSettingStorage.Instance.Language = language.LanguageCode; UserSettingStorage.Instance.Language = language.LanguageCode;
UserSettingStorage.Instance.Save(); UserSettingStorage.Instance.Save();
ResourceMerger.ApplyLanguageResources(); ResourceMerger.ApplyLanguageResources(this);
} }
public ResourceDictionary GetResourceDictionary() public ResourceDictionary GetResourceDictionary()