wenxiangchao
3 天以前 b544d5cccd76735e034c8055918a50927b437474
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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<object, IPlugin> Cache = new Dictionary<object, IPlugin>();
        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);
        }
    }
}