PowerToys/WinAlfred/PluginLoader/CSharpPluginLoader.cs

64 lines
2.0 KiB
C#
Raw Normal View History

2013-12-20 19:38:10 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
2013-12-22 19:35:21 +08:00
using System.Threading;
2013-12-20 19:38:10 +08:00
using WinAlfred.Helper;
using WinAlfred.Plugin;
2014-01-03 23:52:36 +08:00
using WinAlfred.Plugin.System;
2013-12-20 19:38:10 +08:00
namespace WinAlfred.PluginLoader
{
public class CSharpPluginLoader : BasePluginLoader
2013-12-20 19:38:10 +08:00
{
public override List<PluginPair> LoadPlugin()
2013-12-20 19:38:10 +08:00
{
List<PluginPair> plugins = new List<PluginPair>();
List<PluginMetadata> metadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
foreach (PluginMetadata metadata in metadatas)
{
try
{
2013-12-23 23:53:38 +08:00
Assembly asm = Assembly.LoadFile(metadata.ExecuteFilePath);
2014-01-03 23:52:36 +08:00
List<Type> types = asm.GetTypes().Where(o => o.IsClass && o.GetInterfaces().Contains(typeof(IPlugin)) || o.GetInterfaces().Contains(typeof(ISystemPlugin))).ToList();
if (types.Count == 0)
{
2013-12-22 00:44:56 +08:00
Log.Error(string.Format("Cound't load plugin {0}: didn't find the class who implement IPlugin",
metadata.Name));
continue;
}
2014-01-03 23:52:36 +08:00
foreach (Type type in types)
{
2014-01-03 23:52:36 +08:00
PluginPair pair = new PluginPair()
{
Plugin = Activator.CreateInstance(type) as IPlugin,
Metadata = metadata
};
plugins.Add(pair);
}
}
catch (Exception e)
{
Log.Error(string.Format("Cound't load plugin {0}: {1}", metadata.Name, e.Message));
#if (DEBUG)
2013-12-22 00:44:56 +08:00
{
throw;
}
#endif
}
}
2013-12-22 19:35:21 +08:00
InitPlugin(plugins);
2013-12-20 19:38:10 +08:00
return plugins;
}
2013-12-22 19:35:21 +08:00
private void InitPlugin(List<PluginPair> plugins)
{
2014-01-03 23:52:36 +08:00
2013-12-22 19:35:21 +08:00
}
2013-12-20 19:38:10 +08:00
}
}