C#实现动态加载dll的方法
作者:shichen2014 发布时间:2022-11-26 08:15:17
本文实例讲述了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("EMethoddoesnotexist!");
}
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#程序设计有所帮助。


猜你喜欢
- Android Studio 在引用外部依赖时,发现一直无法引用外部依赖。刚开始以为是墙的问题,尝试修改Gradle配置,未解决问题。最终发
- VideoView介绍之前介绍过使用MediaPlayer+SurfaceView实现播放视频功能。无意间发现官方封装了VideoView组
- clone()和Cloneable接口clone顾名思义就是克隆,即,复制一个相等的对象,但是不同的引用地址。我们知道拿到一个对象的地址,只
- 前言自从国产之光fastjson频频暴雷,jackson json的使用是越来越广泛了。尤其是spring家族把它搞成了默认的JSON处理包
- int、String的类型转换int -> Stringint i=12345;String s="";第一种方法
- idea的前后跳转箭头没有像eclipse一样显示在导航栏,很多时候找不到,记住快捷键:ctrl+alt+ 向左箭头或者向右箭头即可跳转。补
- 1.什么是servlet?sun(oracle)公司制订的一种用来扩展web服务器端功能的组件规范。背景:常用的web服务器:apache
- package com.imoyo.expert;import java.util.ArrayList;import android.app
- OO思想现在已经在软件开发项目中广泛应用,其中最重要的一个特性就是继承,最近偶简单的复习了下在C#中涉及到继承这个特性时,所需要用到的关键字
- 1. 什么是JvmJVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来
- 基于JavaFX开发桌面程序注:我也是JAVA FX的初学者之一,自己在学习的时候踩了许多的坑,中文英文的资料查了不少,但是觉得FX技术和其
- 一、场景Java实现文件上传到服务器本地,并通过url访问有个需求,前端上传文件,需要用开关的方式同时支持上传七牛和服务器本地,方便不同的用
- 本文实例为大家分享了C#基于winform实现音乐播放器的具体代码,供大家参考,具体内容如下首先,右键工具箱的组件,找到选择项,找到Wind
- 一、事务的基本原理Spring事务的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring是无法提供事务功能的。对于纯JDBC操
- 文档中添加印章可以起一定的作用,比如,防止文件随意被使用,或者确保文档内容的安全性和权威性。C#添加图片印章其实也有很多实现方法,这里我使用
- 一、JAVA常用APIjava.lang.Math提供sin, cos, tan, exp, log, log10 等类方法,PI和E等类字
- 目录一 为什么要用锁二 synchronized怎么实现的三 CAS来者何人四synchronized和CAS孰优孰劣轻量级锁重量级锁总结提
- 问题用过storm或者jstorm的都知道,如果在bolt代码中发生了没被catch住的异常,所在worker进程会退出。本文就从源码角度分
- 锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchronized 和 ReentrantLock等等 ) 。这些
- 调用海康工业相机SDK采集图像并在Halcon窗口中显示最近做项目需要对海康相机进行二次开发,现将所学进行整理。开发环境 &nbs