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

 更新时间:2014年12月12日 09:41:54   投稿:shichen2014  
这篇文章主要介绍了C#实现动态加载dll的方法,涉及针对动态链接库的灵活操作技巧,具有一定的参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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#程序设计有所帮助。

相关文章

  • c#结构和类的相关介绍

    c#结构和类的相关介绍

    结构和类的共同点都是属于抽象数据类型,包含数据和数据的操作。不同点在于结构偏重于数据语意,而类偏重於行为语意。
    2012-12-12
  • C#实现简单的计算器功能(窗体)

    C#实现简单的计算器功能(窗体)

    这篇文章主要为大家详细介绍了C#实现简单的计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • C#中new和override的区别个人总结

    C#中new和override的区别个人总结

    这篇文章主要介绍了C#中new和override的区别个人总结,本文以问答的方式讲解了new和override的区别,需要的朋友可以参考下
    2015-06-06
  • C#并查集(union-find)算法详解

    C#并查集(union-find)算法详解

    本文详细讲解了C#并查集(union-find)算法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • C#获取应用程序路径或Web页面目录路径

    C#获取应用程序路径或Web页面目录路径

    这篇文章介绍了C#获取应用程序路径或Web页面目录路径的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • Winform基于多线程实现每隔1分钟执行一段代码

    Winform基于多线程实现每隔1分钟执行一段代码

    这篇文章主要介绍了Winform基于多线程实现每隔1分钟执行一段代码的方法,设计线程的操作及时间函数的用法,需要的朋友可以参考下
    2014-10-10
  • SQLite在C#中的安装与操作技巧

    SQLite在C#中的安装与操作技巧

    SQLite,是一款轻型的数据库,用于本地的数据储存。其优点有很多,下面通过本文给大家介绍SQLite在C#中的安装与操作技巧,感兴趣的的朋友参考下吧
    2017-08-08
  • C#-WinForm跨线程修改UI界面的示例

    C#-WinForm跨线程修改UI界面的示例

    这篇文章主要介绍了C#-WinForm跨线程修改UI界面的示例,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2021-01-01
  • WPF中的ListBox实现按块显示元素的方法

    WPF中的ListBox实现按块显示元素的方法

    这篇文章主要介绍了WPF中的ListBox实现按块显示元素的方法,涉及ListBox属性设置相关操作技巧,需要的朋友可以参考下
    2016-09-09
  • C#类的访问修饰符用法分析

    C#类的访问修饰符用法分析

    这篇文章主要介绍了C#类的访问修饰符用法,较为详细的分析了C#类的访问修饰符概念与用法,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-10-10

最新评论