Error[8]: Undefined offset: 3, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

C#如何使用Reflect获取dll文件中的类型并调用? C#如何使用Reflect获取dll文件中的类型并调用??本篇文章就给大家介绍C#使用Reflect(反射)获取dll文件中的类型并调用的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所助。

使用Reflect(反射)获取dll文件中的类型并调用方法,需引用:

System.Reflection;

1. 使用Reflect(反射)获取dll文件中的类型并调用方法的示例(入门案例)

static void Main(string[] args)
        {
            //dll文件路径
            string path = @"D:\VS2015Project
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);
            }
        }
    }
}
1\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文件代码

[+++]

3. 反射调用结果:

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问C#视频教程!

以上就是C#如何使用Reflect获取dll文件中的类型并调用?的详细内容,)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 29, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)

C#如何使用Reflect获取dll文件中的类型并调用?_C_内存溢出

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

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

C#如何使用Reflect获取dll文件中的类型并调用? C#如何使用Reflect获取dll文件中的类型并调用??本篇文章就给大家介绍C#使用Reflect(反射)获取dll文件中的类型并调用的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所助。

使用Reflect(反射)获取dll文件中的类型并调用方法,需引用:

System.Reflection;

1. 使用Reflect(反射)获取dll文件中的类型并调用方法的示例(入门案例)

static void Main(string[] args)
        {
            //dll文件路径
            string path = @"D:\VS2015Project
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);
            }
        }
    }
}
1\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文件代码

3. 反射调用结果:

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问C#视频教程!

以上就是C#如何使用Reflect获取dll文件中的类型并调用?的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/686177.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-20
下一篇 2022-04-20

发表评论

登录后才能评论

评论列表(0条)

保存