本文摘自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#和.net是一个东西吗?C#与.net的区别与联系
页面包含处理实例详解
C#解析xml文件的代码实例分享
C#类的声明详解及实例
C#正则函数匹配、替换、提取的用法代码分享
C# 应用npoi获取excel中的图片,保存至本地的算法的图文代码实例详解
具体介绍C#线程与线程池的区别
详解C#使用litjson解析json的示例代码
C#实现windows剪贴板监视器的代码具体介绍
C#-json详解
更多相关阅读请进入《C#》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » C#如何使用Reflect获取dll文件中的类型并调用?