在 C# 中动态加载插件的类,支持 COM对象 和 Net 的插件。

kehu

使用了应用程序域来加载 COM,本想都采用应用程序域的,但没找到传对象到新域里的方法(如传 DataTable、TcpClient 等主域中的对象),所以 Net 的插件采用了读入内存的方法载入,这样就可以给 Net 的插件传主域中的对象了。
  1 None.gif using  System;
  2 None.gif using  System.Reflection;
  3 None.gif using  System.IO;
  4 None.gif
  5 None.gif
  6 None.gif namespace  Loader
  7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  8InBlock.gif    class LoaderDomain
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 10InBlock.gif        AppDomain appDomain;
 11InBlock.gif        public RemoteLoader remoteLoader;
 12InBlock.gif
 13InBlock.gif        public LoaderDomain(string path)
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 15InBlock.gif
 16InBlock.gif            AppDomainSetup tmpSetup = new AppDomainSetup();
 17InBlock.gif            tmpSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
 18InBlock.gif            tmpSetup.ShadowCopyDirectories = path;
 19InBlock.gif
 20InBlock.gif            appDomain = AppDomain.CreateDomain("newDomain"new System.Security.Policy.Evidence(AppDomain.CurrentDomain.Evidence), tmpSetup);
 21InBlock.gif
 22InBlock.gif            RemoteLoader tmpRemoteLoader = new RemoteLoader();
 23InBlock.gif
 24InBlock.gif            remoteLoader = (RemoteLoader)
 25InBlock.gif                appDomain.CreateInstanceAndUnwrap(tmpRemoteLoader.GetType().Assembly.FullName, tmpRemoteLoader.GetType().FullName);
 26InBlock.gif
 27InBlock.gif            tmpRemoteLoader = null;
 28InBlock.gif
 29ExpandedSubBlockEnd.gif        }

 30InBlock.gif
 31InBlock.gif        public void Unload()
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 33InBlock.gif            AppDomain.Unload(appDomain);
 34InBlock.gif            appDomain = null;
 35ExpandedSubBlockEnd.gif        }

 36InBlock.gif
 37ExpandedSubBlockEnd.gif    }

 38InBlock.gif
 39InBlock.gif    public class RemoteLoader : MarshalByRefObject
 40ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 41InBlock.gif
 42InBlock.gif        Object tmpClass = null;
 43InBlock.gif        Type tmpType = null;
 44InBlock.gif
 45InBlock.gif        public void LoadAssembly(string path, string typeName)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//*tmpClass = Assembly.LoadFile(path).CreateInstance(typeName);
 48ExpandedSubBlockEnd.gif            tmpType = tmpClass.GetType();*/

 49InBlock.gif
 50InBlock.gif            tmpClass = Assembly.Load(File.ReadAllBytes(path)).CreateInstance(typeName); 
 51InBlock.gif            tmpType = tmpClass.GetType();
 52InBlock.gif
 53InBlock.gif
 54ExpandedSubBlockEnd.gif        }

 55InBlock.gif
 56InBlock.gif        public void LoadCOM(string progID)
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 58InBlock.gif            tmpType = Type.GetTypeFromProgID(progID);
 59InBlock.gif            tmpClass = Activator.CreateInstance(tmpType);
 60InBlock.gif
 61ExpandedSubBlockEnd.gif        }

 62InBlock.gif
 63InBlock.gif        public object InvokeMember(string name, BindingFlags invokeAttr, object[] args)
 64ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 65InBlock.gif            if (tmpClass != null && tmpType != null)
 66ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 67InBlock.gif                return tmpType.InvokeMember(name, invokeAttr | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance, null, tmpClass, args);
 68ExpandedSubBlockEnd.gif            }

 69InBlock.gif            else
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 71InBlock.gif                return null;
 72ExpandedSubBlockEnd.gif            }

 73ExpandedSubBlockEnd.gif        }

 74InBlock.gif
 75InBlock.gif        public bool IsMember(string name, MemberTypes type)
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 77InBlock.gif            if (tmpClass != null && tmpType != null)
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 79InBlock.gif                try
 80ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 81InBlock.gif                    if (tmpType.GetMember(name, type, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).Length >= 1)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 83InBlock.gif                        return true;
 84ExpandedSubBlockEnd.gif                    }

 85InBlock.gif                    else
 86ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 87InBlock.gif                        return false;
 88ExpandedSubBlockEnd.gif                    }

 89ExpandedSubBlockEnd.gif                }

 90InBlock.gif                catch
 91ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 92InBlock.gif                    return false;
 93ExpandedSubBlockEnd.gif                }

 94ExpandedSubBlockEnd.gif            }

 95InBlock.gif            else
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 97InBlock.gif                return false;
 98ExpandedSubBlockEnd.gif            }

 99ExpandedSubBlockEnd.gif        }

100InBlock.gif
101ExpandedSubBlockEnd.gif    }

102ExpandedBlockEnd.gif}

103 None.gif
104 None.gif
105 None.gif

调用 COM 插件的方法。
 1 None.gif
 2 None.gif                             // 调用COM插件
 3 None.gif                             LoaderDomain tmpLoaderDomain  =   new  LoaderDomain(AppDomain.CurrentDomain.BaseDirectory);
 4 None.gif                             try
 5 ExpandedBlockStart.gifContractedBlock.gif                             dot.gif {
 6InBlock.gif                                
 7InBlock.gif                                tmpLoaderDomain.remoteLoader.LoadCOM("类名");
 8InBlock.gif
 9InBlock.gif
10ExpandedBlockEnd.gif                            }

11 None.gif                             catch  (Exception err)
12 ExpandedBlockStart.gifContractedBlock.gif                             dot.gif {//创建COM插件。
13InBlock.gif                                MessageBox.Show(err.Message);
14InBlock.gif
15ExpandedBlockEnd.gif                            }

16 None.gif                             finally
17 ExpandedBlockStart.gifContractedBlock.gif                             dot.gif {
18ExpandedSubBlockStart.gifContractedSubBlock.gif                                try dot.gif{ tmpLoaderDomain.Unload(); }
19ExpandedSubBlockStart.gifContractedSubBlock.gif                                catch dot.gif{ }
20InBlock.gif
21ExpandedBlockEnd.gif                            }

22 None.gif
23 None.gif                            tmpLoaderDomain  =   null ;


调用 Net 插件的方法。
 1 None.gif
 2 None.gif
 3 None.gif                             // 调用.Net插件
 4 None.gif
 5 None.gif                             string  tmpClassPath  =   " 插件路径 " ;
 6 None.gif
 7 None.gif                             if  (File.Exists(tmpClassPath))
 8 ExpandedBlockStart.gifContractedBlock.gif                             dot.gif {
 9InBlock.gif
10InBlock.gif                                RemoteLoader tmpRemoteLoader = new RemoteLoader();
11InBlock.gif                                try
12ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
13InBlock.gif                                    tmpRemoteLoader.LoadAssembly(tmpClassPath, "类名");
14InBlock.gif
15ExpandedSubBlockEnd.gif                                }

16InBlock.gif                                catch(Exception  err)
17ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{//创建.Net插件失败
18InBlock.gif                                    MessageBox.Show(err.Message);
19ExpandedSubBlockEnd.gif                                }

20InBlock.gif
21InBlock.gif                                tmpRemoteLoader = null;
22InBlock.gif
23ExpandedBlockEnd.gif                            }

24 None.gif


统一的访问属性、方法。
1 ExpandedBlockStart.gif ContractedBlock.gif tmpRemoteLoader.InvokeMember( " 属性名 " , BindingFlags.SetProperty ,  new  Object[]  dot.gif "参数1""参数2" } );
2 None.gif bool  tmpReturn  =  ( bool )tmpRemoteLoader.InvokeMember( " 方法名 " , BindingFlags.InvokeMethod,  null );



转载于:https://www.cnblogs.com/Jiong/archive/2007/04/05/702001.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值