欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)動態(tài)加載dll的方法

 更新時間:2014年12月12日 09:41:54   投稿:shichen2014  
這篇文章主要介紹了C#實現(xiàn)動態(tài)加載dll的方法,涉及針對動態(tài)鏈接庫的靈活操作技巧,具有一定的參考借鑒價值,需要的朋友可以參考下

本文實例講述了C#實現(xiàn)動態(tài)加載dll的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

復(fù)制代碼 代碼如下:

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)
            {

            }
        }
    }
}

復(fù)制代碼 代碼如下:

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#程序設(shè)計有所幫助。

相關(guān)文章

  • c#結(jié)構(gòu)和類的相關(guān)介紹

    c#結(jié)構(gòu)和類的相關(guān)介紹

    結(jié)構(gòu)和類的共同點都是屬于抽象數(shù)據(jù)類型,包含數(shù)據(jù)和數(shù)據(jù)的操作。不同點在于結(jié)構(gòu)偏重于數(shù)據(jù)語意,而類偏重於行為語意。
    2012-12-12
  • C#實現(xiàn)簡單的計算器功能(窗體)

    C#實現(xiàn)簡單的計算器功能(窗體)

    這篇文章主要為大家詳細介紹了C#實現(xiàn)簡單的計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C#中new和override的區(qū)別個人總結(jié)

    C#中new和override的區(qū)別個人總結(jié)

    這篇文章主要介紹了C#中new和override的區(qū)別個人總結(jié),本文以問答的方式講解了new和override的區(qū)別,需要的朋友可以參考下
    2015-06-06
  • C#并查集(union-find)算法詳解

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

    本文詳細講解了C#并查集(union-find)算法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • C#獲取應(yīng)用程序路徑或Web頁面目錄路徑

    C#獲取應(yīng)用程序路徑或Web頁面目錄路徑

    這篇文章介紹了C#獲取應(yīng)用程序路徑或Web頁面目錄路徑的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • Winform基于多線程實現(xiàn)每隔1分鐘執(zhí)行一段代碼

    Winform基于多線程實現(xiàn)每隔1分鐘執(zhí)行一段代碼

    這篇文章主要介紹了Winform基于多線程實現(xiàn)每隔1分鐘執(zhí)行一段代碼的方法,設(shè)計線程的操作及時間函數(shù)的用法,需要的朋友可以參考下
    2014-10-10
  • SQLite在C#中的安裝與操作技巧

    SQLite在C#中的安裝與操作技巧

    SQLite,是一款輕型的數(shù)據(jù)庫,用于本地的數(shù)據(jù)儲存。其優(yōu)點有很多,下面通過本文給大家介紹SQLite在C#中的安裝與操作技巧,感興趣的的朋友參考下吧
    2017-08-08
  • C#-WinForm跨線程修改UI界面的示例

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

    這篇文章主要介紹了C#-WinForm跨線程修改UI界面的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-01-01
  • WPF中的ListBox實現(xiàn)按塊顯示元素的方法

    WPF中的ListBox實現(xiàn)按塊顯示元素的方法

    這篇文章主要介紹了WPF中的ListBox實現(xiàn)按塊顯示元素的方法,涉及ListBox屬性設(shè)置相關(guān)操作技巧,需要的朋友可以參考下
    2016-09-09
  • C#類的訪問修飾符用法分析

    C#類的訪問修飾符用法分析

    這篇文章主要介紹了C#類的訪問修飾符用法,較為詳細的分析了C#類的訪問修飾符概念與用法,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-10-10

最新評論