c#实现动态加载Dll
2015-11-25 17:38
495 查看
转自:/article/9241879.html
原理如下:
1、利用反射进行动态加载和调用.
Assembly assembly=Assembly.LoadFrom(DllPath); //利用dll的路径加载,同时将此程序集所依赖的程序集加载进来,需后辍名.dll
Assembly.LoadFile 只加载指定文件,并不会自动加载依赖程序集.Assmbly.Load无需后辍名
2、加载dll后,需要使用dll中某类.
Type type=ass.GetType(“TypeName”);//用类型的命名空间和名称获得类型
3、需要实例化类型,才可以使用,参数可以人为的指定,也可以无参数,静态实例可以省略
Object obj = Activator.CreateInstance(type,params[]);//利用指定的参数实例话类型
4、调用类型中的某个方法:
需要首先得到此方法
MethodInfo mi=type.GetMethod(“MehtodName”);//通过方法名称获得方法
5、然后对方法进行调用,多态性利用参数进行控制
mi.Invoke(obj,params[]);//根据参数直线方法,返回值就是原方法的返回值
[c-sharp] view
plaincopy
#region 声明动态载入DLL的参数
object obj=null;
byte[] filesByte;
Assembly assembly;
Type type;
MethodInfo timerInitial;
MethodInfo timerDispose;
#endregion
private void LoadDll()//加载DLL
{
try
{
filesByte = File.ReadAllBytes(Path.GetDirectoryName(Application.ExecutablePath) + "//loadDll.dll");
assembly = Assembly.Load(filesByte);
type = assembly.GetType("test.loadDll");
obj = System.Activator.CreateInstance(type);
timerStart = tp.GetMethod("TimerStart");
timerStop = tp.GetMethod("TimerStop");
if (timerStart != null)
{
timerStart.Invoke(obj, null);
}
}
catch(Exception)
{
}
}
以下摘自MSDN
[c-sharp] view
plaincopy
public class A
{
public virtual int method () {return 0;}
}
public class B
{
public virtual int method () {return 1;}
}
class Mymethodinfo
{
public static int Main()
{
Console.WriteLine ("/nReflection.MethodInfo");
A MyA = new A();
B MyB = new B();
// Get the Type and MethodInfo.
Type MyTypea = Type.GetType("A");
MethodInfo Mymethodinfoa = MyTypea.GetMethod("method");
Type MyTypeb = Type.GetType("B");
MethodInfo Mymethodinfob = MyTypeb.GetMethod("method");
// Get and display the Invoke method.
Console.Write("/nFirst method - " + MyTypea.FullName +
" returns " + Mymethodinfoa.Invoke(MyA, null));
Console.Write("/nSecond method - " + MyTypeb.FullName +
" returns " + Mymethodinfob.Invoke(MyB, null));
return 0;
}
}
http://msdn.microsoft.com/en-us/library/a89hcwhh.aspx
原理如下:
1、利用反射进行动态加载和调用.
Assembly assembly=Assembly.LoadFrom(DllPath); //利用dll的路径加载,同时将此程序集所依赖的程序集加载进来,需后辍名.dll
Assembly.LoadFile 只加载指定文件,并不会自动加载依赖程序集.Assmbly.Load无需后辍名
2、加载dll后,需要使用dll中某类.
Type type=ass.GetType(“TypeName”);//用类型的命名空间和名称获得类型
3、需要实例化类型,才可以使用,参数可以人为的指定,也可以无参数,静态实例可以省略
Object obj = Activator.CreateInstance(type,params[]);//利用指定的参数实例话类型
4、调用类型中的某个方法:
需要首先得到此方法
MethodInfo mi=type.GetMethod(“MehtodName”);//通过方法名称获得方法
5、然后对方法进行调用,多态性利用参数进行控制
mi.Invoke(obj,params[]);//根据参数直线方法,返回值就是原方法的返回值
[c-sharp] view
plaincopy
#region 声明动态载入DLL的参数
object obj=null;
byte[] filesByte;
Assembly assembly;
Type type;
MethodInfo timerInitial;
MethodInfo timerDispose;
#endregion
private void LoadDll()//加载DLL
{
try
{
filesByte = File.ReadAllBytes(Path.GetDirectoryName(Application.ExecutablePath) + "//loadDll.dll");
assembly = Assembly.Load(filesByte);
type = assembly.GetType("test.loadDll");
obj = System.Activator.CreateInstance(type);
timerStart = tp.GetMethod("TimerStart");
timerStop = tp.GetMethod("TimerStop");
if (timerStart != null)
{
timerStart.Invoke(obj, null);
}
}
catch(Exception)
{
}
}
以下摘自MSDN
[c-sharp] view
plaincopy
public class A
{
public virtual int method () {return 0;}
}
public class B
{
public virtual int method () {return 1;}
}
class Mymethodinfo
{
public static int Main()
{
Console.WriteLine ("/nReflection.MethodInfo");
A MyA = new A();
B MyB = new B();
// Get the Type and MethodInfo.
Type MyTypea = Type.GetType("A");
MethodInfo Mymethodinfoa = MyTypea.GetMethod("method");
Type MyTypeb = Type.GetType("B");
MethodInfo Mymethodinfob = MyTypeb.GetMethod("method");
// Get and display the Invoke method.
Console.Write("/nFirst method - " + MyTypea.FullName +
" returns " + Mymethodinfoa.Invoke(MyA, null));
Console.Write("/nSecond method - " + MyTypeb.FullName +
" returns " + Mymethodinfob.Invoke(MyB, null));
return 0;
}
}
http://msdn.microsoft.com/en-us/library/a89hcwhh.aspx
相关文章推荐
- C# 关于Get和Set的疑惑
- C# 继承
- C#读取设置Cookie
- C#
- csharp:百度语音识别
- C#Winfrom如何测试当前设备是否处理联网状态。
- C#数据类型
- csharp: Sound recording
- c#字符相似度对比
- C# 获取DataTable数据导出到Excel
- c# 替换非法字符
- C#单例模式
- C#指定日期为一年中的第几周
- 支付宝移动支付开发详细教程服务端采用.net mvc webapi(C#)
- C#关于AutoResetEvent的使用介绍----修正
- VS2010 正则替换
- C# 之 Linq 学习笔记
- c#数据类型的值传递和引用传递--基础拾遗
- C# 如何判断字符串中是否包含另一个字符串?
- C# 添加,修改,删除Xml节点