chr
7 天以前 43a0207d207390abdeeb3ab9155eebf03edd7b1a
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.IO;
using System.Reflection;
 
namespace PdmSwPlugin.Common
{
    public class RemoteLoader : MarshalByRefObject
    {
        private Assembly _assembly;
 
        public void LoadAssembly(string assemblyFile)
        {
            _assembly = Assembly.LoadFrom(assemblyFile);
        }
        public dynamic GetInstance(string typeName)
        {
            if (_assembly == null) return null;
            var type = _assembly.GetType(typeName);
            var obj = Activator.CreateInstance(type);
            return obj;
        }
        public object ExecuteMothod(string typeName, string methodName, object[] parameters)
        {
            if (_assembly == null) return null;
            var type = _assembly.GetType(typeName);
            var obj = Activator.CreateInstance(type);
            // return obj;
            MethodInfo method = type.GetMethod(methodName);
            return method.Invoke(obj, parameters);
        }
    }
 
    public class PluginLoader
    {
        public AppDomain appDomain { get; set; }
        public RemoteLoader remoteLoader { get; set; }
        public PluginLoader(string pluginName)
        {
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationName = "plugin_" + pluginName;
            setup.ApplicationBase = "C:\\Users\\cheng\\Documents\\Workspace\\PdmSwPlugin.MaterialSelect\\bin\\Debug";// "C:\\Users\\cheng\\Documents\\Workspace\\PdmSw\\bin\\x64\\Debug";
            setup.ConfigurationFile = "C:\\Users\\cheng\\Documents\\Workspace\\PdmSw\\bin\\x64\\Debug\\PdmSwPlugin.Main.dll.config";
 
                //"C:\\Users\\cheng\\Documents\\Workspace\\WpfUI\\bin\\x64\\Debug\\WpfUI.exe.config";
            setup.CachePath = setup.ApplicationBase;
            //setup.ShadowCopyFiles = "true";
            //setup.ShadowCopyDirectories = setup.ApplicationBase;
            setup.PrivateBinPath = "C:\\Users\\cheng\\Documents\\Workspace\\PdmSwPlugin.MaterialSelect\\bin\\Debug"; // AppDomain.CurrentDomain.BaseDirectory;
            // setup.DynamicBase = "C:\\Work\\dll\\dll"; // AppDomain.CurrentDomain.BaseDirectory;
            appDomain = AppDomain.CreateDomain("plugin_" + pluginName, null, setup);
 
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
 
            string name = Assembly.GetAssembly(typeof(RemoteLoader)).GetName().FullName;
            string className = typeof(RemoteLoader).FullName;
            object sb = appDomain.CreateInstanceAndUnwrap(name, className);
            remoteLoader = (RemoteLoader)sb;
            //appDomain.Load("C:\\Users\\cheng\\Documents\\Workspace\\PdmSwPlugin.MaterialSelect\\bin\\Debug\\PdmSwPlugin.MaterialSelect.dll");
            remoteLoader.LoadAssembly("C:\\Users\\cheng\\Documents\\Workspace\\PdmSwPlugin.MaterialSelect\\bin\\Debug\\PdmSwPlugin.MaterialSelect.dll");
        }
 
        public Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            try
            {
                Assembly assembly = System.Reflection.Assembly.Load(args.Name);
                if (assembly != null) return assembly;
            }
            catch
            { // ignore load error
            }
 
            // *** Try to load by filename - split out the filename of the full assembly name
            // *** and append the base path of the original assembly (ie. look in the same dir)
            // *** NOTE: this doesn't account for special search paths but then that never
            //           worked before either.
            string[] Parts = args.Name.Split(',');
            string File = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + Parts[0].Trim() + ".dll";
            return System.Reflection.Assembly.LoadFrom(File);
        }
        /// <summary>
        /// 加载程序集
        /// </summary>
        /// <param name="assemblyFile"></param>
        public void LoadAssembly(string assemblyFile)
        {
            remoteLoader.LoadAssembly(assemblyFile);
        }
        /// <summary>
        /// 创建对象实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public dynamic GetInstance(string typeName)
        {
            if (remoteLoader == null) return null;
            return remoteLoader.GetInstance(typeName);
        }
        /// <summary>
        /// 执行类型方法
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="methodName"></param>
        /// <returns></returns>
        public object ExecuteMothod(string typeName, string methodName, object[] parameters)
        {
           return remoteLoader.ExecuteMothod(typeName, methodName, parameters);
        }
        /// <summary>
        /// 卸载应用程序域
        /// </summary>
        public void Unload()
        {
            try
            {
                if (appDomain == null) return;
                AppDomain.Unload(appDomain);
                appDomain = null;
                remoteLoader = null;
            }
            catch (CannotUnloadAppDomainException ex)
            {
                throw ex;
            }
        }
        public Assembly[] GetAssemblies()
        {
            AppDomain a = this.appDomain;
            return a.GetAssemblies();
        }
    }
}