C#实现动态加载dll的方法
本文实例讲述了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("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#程序设计有所帮助。