public void RemovePlugin <T>() { foreach (var item in TaggedPlugins) { if (typeof(T) != item.Instance.GetType()) { continue; } TaggedPlugins.Remove(item); } }
protected virtual void InsertPlugin(Type type, StartupConfig startupConfig) { try { PluginBase plugin = (PluginBase)Activator.CreateInstance(type); string pluginType, error = "", commands = ""; switch (plugin.PluginType) { case PluginType.Command: pluginType = "命令"; CommandPlugin cmdPlugin = (CommandPlugin)plugin; if (cmdPlugin.Commands != null && cmdPlugin.Commands.Length > 0) { foreach (var cmd in cmdPlugin.Commands) { TaggedPlugins.Add(new TaggedClass <PluginBase>(cmd, cmdPlugin)); CachedCommands.Add(new TaggedClass <Type>(cmd, type)); } commands = $"({string.Join(",", cmdPlugin.Commands)}) "; } else { error = "但此命令插件未设置命令。"; } break; case PluginType.Unknown: throw new NotSupportedException(); case PluginType.Application: case PluginType.Service: default: pluginType = plugin.PluginType == PluginType.Application ? "应用" : "服务"; TaggedPlugins.Add(new TaggedClass <PluginBase>(null, plugin)); break; } plugin.OnInitialized(startupConfig); AllPluginInitialized += plugin.AllPlugins_Initialized; Logger.Origin($"{pluginType} \"{plugin.Name}\" {commands}已经加载完毕。{error}"); } catch (Exception ex) { Logger.Exception(ex.InnerException ?? ex); Logger.Error($"加载插件{type.Name}失败。"); } }
public T GetPlugin <T>() where T : PluginBase { return((T)TaggedPlugins.FirstOrDefault(k => k.Instance.GetType() == typeof(T)).Instance); }