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#程序设计有所帮助。
猜你喜欢
- 前天在做批量数据导入新增时,要对数据进行有效性判断,其中还要去除重复,如果没出现linq的话可能会新声明一个临时对象集合,然后遍历原始数据判
- 一、参数校验springboot 使用校验框架validation校验方法的入参SpringBoot的Web组件内部集成了hibernate
- 前段时间写了一篇C#解析Lrc歌词文件,对lrc文件进行解析,支持多个时间段合并。本文借下载歌词文件来探讨一下同步和异步方法。 L
- 1、异常分类通常分为三类:系统异常(SystemException),业务异常(BusinessException)和其他异常(Except
- 前言图文并茂的内容往往让人看起来更加舒服,如果只是文字内容的累加,往往会使读者产生视觉疲劳。搭配精美的文章配图则会使文章内容更加丰富,增加文
- Springboot 内置tomcat禁止不安全HTTP方法1、在tomcat的web.xml中可以配置如下内容让tomcat禁止不安全的H
- 1、回顾一下JDK * 的核心参数如果我们要为target类创建一个【JDK * 对象】,那么我们必须要传入如下三个核心参数加载targ
- 之前我们在使用vue进行 h5 表单录入的过程中,遇到了Android软键盘弹出,覆盖 h5页面 输入框 问题,在此进行回顾并分享给大家:系
- using System; using System.Collections; using System.Text; using Syste
- 这篇文章主要介绍了Spring Batch批处理框架使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- IDEA打成jar包并在windows后台运行一、IDEA打成jar包1、File=>Project Structure=>Pr
- 在项目中,都会用到ListView或GridView等列表控件。一般会用来展示从网络请求的数据 。如果请求的数据为空或者在请求的时候正好无没
- 一、基本定义Arrays类,全路径java.util.Arrays,主要功能为操作数组,Arrays类的所有方法均为静态方法,所以调用方式全
- 前言本文准确来讲是探讨如何用 Jackson 来序列化 Apache avro 对象,因为简单用 Jackson 来序列化 Apache a
- .net 是一种多语言的平台,开发.net 可以用多达几十种语言进行。  
- WPF前台代码展示<Window.Resources> <local:Source x:Key=
- 1. 前言在Java开发中接触的开发者大多数不太注重对接口的测试,结果在联调对接中出现各种问题。也有的使用Postman等工具进行测试,虽然
- 项目完整代码链接:代码链接跨服务上传文件示意图一、创建项目springboot:2.2.6JDK:1.8由于资源有限,就用不同端口表示不同服
- 本文实例为大家分享了Android拼图小游戏的具体代码,供大家参考,具体内容如下1、效果图:运行时:结束时:2、PuzzleLayoutVi
- 本文实例为大家分享了java实现学生成绩档案管理系统的具体代码,供大家参考,具体内容如下实验要求• 学生信息录入,信息包括学号、姓名、专业、