您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

C#如何使用Reflect获取dll文件中的类型并调用?

2024/3/28 4:09:21发布20次查看
c#如何使用reflect获取dll文件中的类型并调用??本篇文章就给大家介绍c#使用reflect(反射)获取dll文件中的类型并调用的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所助。
使用reflect(反射)获取dll文件中的类型并调用方法,需引用:
system.reflection;
1. 使用reflect(反射)获取dll文件中的类型并调用方法的示例(入门案例)
static void main(string[] args) { //dll文件路径 string path = @"d:\vs2015project\001\computer\bin\debug\computer.dll"; //加载dll文件 assembly asm = assembly.loadfile(path); //获取类 type type = asm.gettype("computer.computer"); //创建该类型的实例 object obj = activator.createinstance(type); //获取该类的方法 methodinfo mf = type.getmethod("showdrives"); //调用方法 mf.invoke(obj, null); console.readkey(); }
2. 生成类库(computer.dll)的computer.cs文件代码
using system;using system.collections.generic;using system.io;using system.linq;using system.text;namespace computer{ public class computer { private driveinfo[] drives; public computer() { this.drives = driveinfo.getdrives(); } public void showdrives() { console.writeline("该电脑的磁盘驱动器有:\r\n"); foreach (var item in drives) { console.writeline(item); } } }}
3. 反射调用结果:
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问c#视频教程!
以上就是c#如何使用reflect获取dll文件中的类型并调用?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product