手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#实现动态加载dll的方法
C#实现动态加载dll的方法
摘要:本文实例讲述了C#实现动态加载dll的方法。分享给大家供大家参考。具体实现方法如下:复制代码代码如下:usingSystem;usingSy...

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

【C#实现动态加载dll的方法】相关文章:

C# byte数组与Image相互转换的方法

C# dynamic关键字的使用方法

C#处理JPEG头信息的方法

自定义实现Json字符串向C#对象转变的方法

C#读写文件的方法汇总

C#计算代码执行时间的方法

C#定位txt指定行的方法小例子

在Winform动态启动、控制台命令行的方法

解决C#全屏幕截图的实现方法

C# 获取打印机当前状态的方法

精品推荐
分类导航