C产品在市场上有哪些独特优势?
- 内容介绍
- 文章标签
- 相关推荐
本文共计339个文字,预计阅读时间需要2分钟。
使用反射(Reflect)获取dll文件中的类型并调用方法,具体内容如下:
csharpusing System.Reflection;
static void Main(string[] args){ // 获取dll文件路径 string dllPath=@path\to\your\assembly.dll;
// 加载dll文件 Assembly assembly=Assembly.LoadFrom(dllPath);
// 获取指定类型 Type type=assembly.GetType(Namespace.TypeName);
// 获取类型的方法 MethodInfo method=type.GetMethod(MethodName);
// 创建实例 object instance=Activator.CreateInstance(type);
// 调用方法 method.Invoke(instance, null);}
使用反射(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. 反射调用结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计339个文字,预计阅读时间需要2分钟。
使用反射(Reflect)获取dll文件中的类型并调用方法,具体内容如下:
csharpusing System.Reflection;
static void Main(string[] args){ // 获取dll文件路径 string dllPath=@path\to\your\assembly.dll;
// 加载dll文件 Assembly assembly=Assembly.LoadFrom(dllPath);
// 获取指定类型 Type type=assembly.GetType(Namespace.TypeName);
// 获取类型的方法 MethodInfo method=type.GetMethod(MethodName);
// 创建实例 object instance=Activator.CreateInstance(type);
// 调用方法 method.Invoke(instance, null);}
使用反射(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. 反射调用结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

