本文摘自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#时间戳和js时间戳互转方法的代码分享
详细介绍用C#描述数据结构3:arraylist的图文代码
C#实现关闭子窗口和关闭父窗口的案例
详细介绍C#消息提示框messagebox的使用
C#中反射是什么?
C#thread同步mutex的代码详解
浅谈C# 之 hashtable 与 dictionary的代码实例
C#中guid生成格式的四种方法的示例代码分享
详细概述C#中的常用字符串方法
C#正则函数匹配、替换、提取的用法代码分享
更多相关阅读请进入《C#》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » C#如何使用Reflect获取dll文件中的类型并调用?