2024年5月8日发(作者:看吧播放器app下载安装)
c#实现动态加载Dll
原理如下:
1、利用反射进行动态加载和调用.
Assembly ass=om(DllPath); //利用dll的路径加载,同时将此程序集所依赖的
程序集加载进来,需后辍名.dll
le 只加载指定文件,并不会自动加载依赖程序集.无需后辍名
2、加载dll后,需要使用dll中某类.
Type type=e(“TypeName”);//用类型的命名空间和名称获得类型
3、需要实例化类型,才可以使用,参数可以人为的指定,也可以无参数,静态实例可以省略
Object obj = Instance(type,params[]);//利用指定的参数实例话类型
4、调用类型中的某个方法:
需要首先得到此方法
MethodInfo mi=hod(“MehtodName”);//通过方法名称获得方法
5、然后对方法进行调用,多态性利用参数进行控制
(obj,params[]);//根据参数直线方法,返回值就是原方法的返回值
#region 声明动态载入DLL的参数
object obj=null;
byte[] filesByte;
Assembly assembly;
Type type;
MethodInfo timerInitial;
MethodInfo timerDispose;
#endregion
private void LoadDll()//加载DLL
{
try
{
filesByte
lBytes(ectoryName(ablePath) + "");
=
assembly = (filesByte);
type = e("l");
obj = Instance(type);
timerStart = hod("TimerStart");
timerStop = hod("TimerStop");
if (timerStart != null)
{
(obj, null);
}
}
catch(Exception)
{
}
}
以下摘自MSDN
public class A
{
public virtual int method () {return 0;}
}
public class B
{
public virtual int method () {return 1;}
}
class Mymethodinfo
{
public static int Main()
{
ine ("Info");
A MyA = new A();
B MyB = new B();
// Get the Type and MethodInfo.
Type MyTypea = e("A");
MethodInfo Mymethodinfoa = hod("method");
Type MyTypeb = e("B");
MethodInfo Mymethodinfob = hod("method");
// Get and display the Invoke method.
("nFirst method - " + me +
" returns " + (MyA, null));
("nSecond method - " + me +
" returns " + (MyB, null));
return 0;
}
发布者:admin,转转请注明出处:http://www.yc00.com/xitong/1715149675a2573633.html
评论列表(0条)