c# 如何调用指定文件夹里面的动态库dll

最近写代码时遇到了调用指定文件夹里面的dll的情况,在此总结一下:

一、我这里使用的方法是,Assembly 和 MethodInfo,代码如下:

Assembly myAssembly = Assembly.LoadFrom(dllPath);//dllPath是dll文件所在的文件夹
Type myType = myAssembly.GetType("mydll.TestCall");//mydll:namespace的名字,TestCall:类名
 object myInstance = Activator.CreateInstance(myType);

MethodInfo myMethod = myType.GetMethod("testFunc");//testFunc:函数名
            object returnValue = myMethod.Invoke(myInstance, new string[] { "", "" }); // 如果不需要参数,使用null,否则提供适当的参数数组,这里函数testFunc是两个string类型的参数

返回值returnValue,如果没有返回值,返回的是null

二、如果dll文件里面有重载函数话,处理如下,主要是GetMethod函数需要变动:

比如函数testFunc存在两个string参数 和 三个string参数的重载函数,那么,调用三个参数时,代码如下:

MethodInfo myMethod2 = myType.GetMethod("testFunc", new Type[] { typeof(String), typeof(String), typeof(String) });//匹配调用函数的参数类型和个数

相关推荐

  1. c# 如何调用指定文件夹里面动态dll

    2024-04-03 11:06:03       14 阅读
  2. c++动态调用dll

    2024-04-03 11:06:03       41 阅读
  3. Go语言中,如何调用C++dll文件

    2024-04-03 11:06:03       24 阅读
  4. C#动态调用C++DLL函数

    2024-04-03 11:06:03       38 阅读
  5. C++由动态链接dll生成lib文件

    2024-04-03 11:06:03       19 阅读
  6. c#程序调用c++开发dll

    2024-04-03 11:06:03       13 阅读
  7. 实现动态链接DLL)注入C++编程

    2024-04-03 11:06:03       43 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-03 11:06:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-03 11:06:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-03 11:06:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-03 11:06:03       20 阅读

热门阅读

  1. css设置宽高等比 设置和宽度一样的高度

    2024-04-03 11:06:03       15 阅读
  2. NFS环境搭建与在K8s的应用

    2024-04-03 11:06:03       11 阅读
  3. k8s Calico 网络模式总结

    2024-04-03 11:06:03       26 阅读
  4. k8s 安装

    2024-04-03 11:06:03       15 阅读
  5. 4.2作业

    4.2作业

    2024-04-03 11:06:03      15 阅读
  6. 【蓝桥杯第十一届省赛B】(部分详解)

    2024-04-03 11:06:03       16 阅读
  7. C++经典面试题目(二十)

    2024-04-03 11:06:03       13 阅读
  8. 【BUG】vue中@change时间传值丢失问题

    2024-04-03 11:06:03       16 阅读
  9. 什么是原生IP和广播IP

    2024-04-03 11:06:03       15 阅读
  10. 【python】网络爬虫基础

    2024-04-03 11:06:03       15 阅读
  11. STC8H8K64U 学习笔记 - 与上位机通信

    2024-04-03 11:06:03       12 阅读
  12. 深入了解Flutter中Overlay的介绍以及使用

    2024-04-03 11:06:03       15 阅读