本文摘自PHP中文网,作者青灯夜游,侵删。
C#如何使用Reflect获取dll文件中的类型并调用??本篇文章就给大家介绍C#使用Reflect(反射)获取dll文件中的类型并调用的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所助。使用Reflect(反射)获取dll文件中的类型并调用方法,需引用:
1. 使用Reflect(反射)获取dll文件中的类型并调用方法的示例(入门案例)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | static void Main(string[] args)
{
string path = @ "D:\VS2015Project\001\Computer\bin\Debug\computer.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文件代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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文件中的类型并调用?的详细内容!
相关阅读 >>
详细介绍C#该行已经属于另一个表的解决方法
关于C#中方法的阐述
C#中的类型系统(值类型和引用类型)的简单介绍
C# sleep延时方法
C#中如何取绝对值函数的方法详解
详解C#使用litjson解析json的示例代码
C#之正则表达式介绍
C# winform程序上传图片到指定目录的示例代码
asp.net实现分页(非控件,输出html代码)
详解kotlin中如何实现类似java或C#中的静态方法
更多相关阅读请进入《C#》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » C#如何使用Reflect获取dll文件中的类型并调用?