欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现动态加载dll的方法

程序员文章站 2023-12-15 19:09:46
本文实例讲述了c#实现动态加载dll的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下: using system; using system.co...

本文实例讲述了c#实现动态加载dll的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:

using system;
using system.collections.generic;
using system.text;
using system.reflection;
using system.io;

namespace alif.commonapi.dynamicloadassembly
{
    public class assemblydynamicloader<t>
    {
        private appdomain appdomain;

        private dynamicremoteloadassembly<t> remoteloader;

        public t invokemethod(string assemblyname, string assemblypath, string assemblyconfigfilepath, string fullclassname, string methodname, params object[] args)
        {
            appdomainsetup setup = new appdomainsetup();
            setup.applicationname = "applicationloader";
            setup.applicationbase = appdomain.currentdomain.basedirectory + @"bin\";
            //setup.privatebinpath = path.combine(appdomain.currentdomain.basedirectory, "private");
            setup.cachepath = setup.applicationbase;
            setup.shadowcopyfiles = "true";
            if (assemblyconfigfilepath != string.empty)
            {
                setup.configurationfile = appdomain.currentdomain.basedirectory + assemblyconfigfilepath;
            }
            setup.shadowcopydirectories = setup.applicationbase;
            setup.loaderoptimization = loaderoptimization.singledomain;

            this.appdomain = appdomain.createdomain("applicationloaderdomain", null, setup);
            string name = assembly.getexecutingassembly().getname().fullname;

            this.remoteloader = (dynamicremoteloadassembly<t>)this.appdomain.createinstanceandunwrap(name, typeof(dynamicremoteloadassembly<t>).fullname);

            assemblyname = appdomain.currentdomain.basedirectory + assemblypath + assemblyname;

            return this.remoteloader.invokemethod(assemblyname, fullclassname, methodname, args);
        }

        /// <summary>
        ///
        /// </summary>
        public void unload()
        {
            try
            {
                appdomain.unload(this.appdomain);
                this.appdomain = null;
            }
            catch (cannotunloadappdomainexception ex)
            {

            }
        }
    }
}

复制代码 代码如下:

using system;
using system.collections.generic;
using system.text;
using system.reflection;
using system.globalization;

namespace alif.commonapi.dynamicloadassembly
{
    public class dynamicremoteloadassembly<t> : marshalbyrefobject
    {
        private assembly assembly = null;

        public t invokemethod(string assemblypath, string fullclassname, string methodname, params object[] args)
        {
            this.assembly = null;
            t result = default(t);
            try
            {
                this.assembly = assembly.loadfile(assemblypath);
                type pgmtype = null;
                if (this.assembly != null)
                {
                    pgmtype = this.assembly.gettype(fullclassname, true, true);
                }
                else
                {
                    pgmtype = type.gettype(fullclassname, true, true);
                }
                bindingflags defaultbinding = bindingflags.declaredonly | bindingflags.public | bindingflags.nonpublic | bindingflags.instance | bindingflags.ignorecase | bindingflags.invokemethod | bindingflags.static;
                cultureinfo cultureinfo = new cultureinfo("es-es", false);
                try
                {
                    methodinfo methisinfo = assembly.gettype(fullclassname, true, true).getmethod(methodname);

                    if (methisinfo == null)
                    {
                        new exception("emethod does not exist!");
                    }

                    if (methisinfo.isstatic)
                    {
                        if (methisinfo.getparameters().length == 0)
                        {
                            if (methisinfo.returntype == typeof(void))
                            {
                                pgmtype.invokemember(methodname, defaultbinding, null, null, null, cultureinfo);
                            }
                            else
                            {
                                result = (t)pgmtype.invokemember(methodname, defaultbinding, null, null, null, cultureinfo);
                            }
                        }
                        else
                        {
                            if (methisinfo.returntype == typeof(void))
                            {
                                pgmtype.invokemember(methodname, defaultbinding, null, null, args, cultureinfo);
                            }

                            else
                            {
                                result = (t)pgmtype.invokemember(methodname, defaultbinding, null, null, args, cultureinfo);
                            }
                        }
                    }
                    else
                    {

                        if (methisinfo.getparameters().length == 0)
                        {
                            object pgmclass = activator.createinstance(pgmtype);
                            if (methisinfo.returntype == typeof(void))
                            {
                                pgmtype.invokemember(methodname, defaultbinding, null, pgmclass, null, cultureinfo);
                            }
                            else
                            {
                                result = (t)pgmtype.invokemember(methodname, defaultbinding, null, pgmclass, null, cultureinfo);
                            }
                        }
                        else
                        {
                            object pgmclass = activator.createinstance(pgmtype);
                            if (methisinfo.returntype == typeof(void))
                            {
                                pgmtype.invokemember(methodname, defaultbinding, null, pgmclass, args, cultureinfo);
                            }
                            else
                            {
                                result = (t)pgmtype.invokemember(methodname, defaultbinding, null, pgmclass, args, cultureinfo);
                            }
                        }
                    }
                }
                catch (exception e)
                {
                    result = (t)pgmtype.invokemember(methodname, defaultbinding, null, null, null, cultureinfo);
                }
                return result;
            }
            catch (exception ee)
            {
                return result;
            }
        }
    }
}

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: