C#實(shí)現(xiàn)動(dòng)態(tài)加載dll的方法
本文實(shí)例講述了C#實(shí)現(xiàn)動(dòng)態(tài)加載dll的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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;
}
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C# WPF如何反射加載Geometry幾何圖形數(shù)據(jù)圖標(biāo)
- c# WPF中自定義加載時(shí)實(shí)現(xiàn)帶動(dòng)畫效果的Form和FormItem
- c# 實(shí)現(xiàn)網(wǎng)頁加載后將頁面截取為長(zhǎng)圖片
- C# 根據(jù)表格偶數(shù)、奇數(shù)加載不同顏色
- C# 動(dòng)態(tài)加載程序集信息
- C#中調(diào)用DLL時(shí)未能加載文件或程序集錯(cuò)誤的處理方法(詳解)
- C#中加載dll并調(diào)用其函數(shù)的實(shí)現(xiàn)方法
- c# 動(dòng)態(tài)加載dll文件,并實(shí)現(xiàn)調(diào)用其中的簡(jiǎn)單方法
- C#使用Jquery zTree實(shí)現(xiàn)樹狀結(jié)構(gòu)顯示 異步數(shù)據(jù)加載
- C#使用反射加載多個(gè)程序集的實(shí)現(xiàn)方法
- c#動(dòng)態(tài)加載卸載DLL的方法
- 3種C# 加載Word的方法
相關(guān)文章
C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能(窗體)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01C#中new和override的區(qū)別個(gè)人總結(jié)
這篇文章主要介紹了C#中new和override的區(qū)別個(gè)人總結(jié),本文以問答的方式講解了new和override的區(qū)別,需要的朋友可以參考下2015-06-06Winform基于多線程實(shí)現(xiàn)每隔1分鐘執(zhí)行一段代碼
這篇文章主要介紹了Winform基于多線程實(shí)現(xiàn)每隔1分鐘執(zhí)行一段代碼的方法,設(shè)計(jì)線程的操作及時(shí)間函數(shù)的用法,需要的朋友可以參考下2014-10-10WPF中的ListBox實(shí)現(xiàn)按塊顯示元素的方法
這篇文章主要介紹了WPF中的ListBox實(shí)現(xiàn)按塊顯示元素的方法,涉及ListBox屬性設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2016-09-09