using log4net; using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; namespace HMI.Core.Api { public class PluginPackage : IPlugin { public static Dictionary Cache = new Dictionary(); public static IPlugin Get(object obj) { if (obj is null) return null; if (obj is IPlugin plugin) { return plugin; } if (Cache.ContainsKey(obj)) return Cache[obj]; PluginPackage pluginPackage = new PluginPackage(obj); Cache.Add(obj, pluginPackage); return pluginPackage; } private object Content; private MethodInfo ActiveMethod; private MethodInfo DeActiveMethod; private MethodInfo DoCloseMethod; private MethodInfo InitMethod; private MethodInfo InitFinishMethod; private MethodInfo OnCloseMethod; private MethodInfo OnTabChangeMethod; private MethodInfo SetBaseFolderMethod; private MethodInfo SetLoggerMethod; private MethodInfo CanDeActiveMethod; private MethodInfo CanActiveMethod; public PluginPackage(object content) { this.Content = content; ActiveMethod = content.GetType().GetMethod("Active"); DeActiveMethod = content.GetType().GetMethod("DeActive"); DoCloseMethod = content.GetType().GetMethod("DoClose", new Type[] { typeof(object), typeof(EventArgs) }); InitMethod = content.GetType().GetMethod("Init"); InitFinishMethod = content.GetType().GetMethod("InitFinish"); OnCloseMethod = content.GetType().GetMethod("OnClose", new Type[] { typeof(object), typeof(CancelEventArgs) }); OnTabChangeMethod = content.GetType().GetMethod("OnTabChange", new Type[] { typeof(object), typeof(object) }); SetBaseFolderMethod = content.GetType().GetMethod("SetBaseFolder", new Type[] { typeof(string) }); SetLoggerMethod = content.GetType().GetMethod("SetLogger", new Type[] { typeof(ILog) }); CanDeActiveMethod = content.GetType().GetMethod("CanDeActive"); CanActiveMethod = content.GetType().GetMethod("CanActive"); } public void Active() { ActiveMethod?.Invoke(Content, null); } public void DeActive() { DeActiveMethod?.Invoke(Content, null); } public void DoClose(object sender, EventArgs e) { DoCloseMethod?.Invoke(Content, new object[] { sender, e }); } public void Init() { InitMethod?.Invoke(Content, null); } public void InitFinish() { InitFinishMethod?.Invoke(Content, null); } public void OnClose(object sender, CancelEventArgs e) { OnCloseMethod?.Invoke(Content, new object[] { sender, e }); } public void OnTabChange(object sender, object args) { OnTabChangeMethod?.Invoke(Content, new object[] { sender, args }); } public void SetBaseFolder(string folder) { SetBaseFolderMethod?.Invoke(Content, new object[] { folder }); } public void SetLogger(ILog logger) { SetLoggerMethod?.Invoke(Content, new object[] { logger }); } public bool CanActive() { return (bool)CanActiveMethod?.Invoke(Content, null); } public bool CanDeActive() { return (bool)CanDeActiveMethod?.Invoke(Content, null); } } }