C# 加载Dll 使用反射 调用对应的方法公开方法,私有方法,泛型方法

加载Dll三种方式介绍

//有三种方式实现加载程序集
Assembly configExtendsAssembly1 = Assembly.LoadFrom("ConfigExtends.dll");
//需要DLL名称,版本号,公钥 如何获取,可以参考这篇文章 https://qastack.cn/programming/1710935/how-do-i-find-the-publickeytoken-for-a-particular-dll
Assembly configExtendsAssembly2 = Assembly.Load(@"ConfigExtends, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
//从头写到尾 全路径
Assembly configExtendsAssembly3 = Assembly.LoadFile(@"C:\Users\Administrator\source\repos\Study.Reflect\ConfigHandle\bin\Debug\netcoreapp3.1\ConfigExtends.dll"); 

项目下载地址

加载对应的DLL

//需要把对应的dll文件拷贝到当前运行的netcoreapp3.1文件夹中 
Assembly configExtendsAssembly1 = Assembly.LoadFrom("ConfigExtends.dll");
Assembly printReflectionTestAssembly2 = Assembly.LoadFrom("PrintReflectionTest.dll");

反射调用方法

//指定类
var type = configExtendsAssembly1.GetType("ConfigExtends.ConfigService");
//使用指定类创建实例
var oResult = Activator.CreateInstance(type);
//获取指定的方法
var configServicePrint = type.GetMethod("Print", BindingFlags.Public | BindingFlags.Static);
//调用方法
var print = configServicePrint.Invoke(oResult, new[] { "这是打印方法." });
Console.WriteLine(print.ToString());

var configServiceGetConfig = type.GetMethod("GetConfig", BindingFlags.Public | BindingFlags.Static);
/*
* 调用这个获取配置文件的方法里面有个坑,
* 由于上面只是加载了ConfigExtends.dll,其中这个ConfigExtends.ConfigService这个类,但是没有加载相关引用的nuget包
* 所以,当前的程序需要添加ConfigExtends.ConfigService需要使用到的nuget包
*/
var connectionStrings = configServiceGetConfig.Invoke(oResult, new[] { "ConnectionStrings:defulat" });

Console.WriteLine(connectionStrings?.ToString());

获取反射类型里各种方法(公开的,私有的,公开静态的,内部的)

//获取指定类
var type = printReflectionTestAssembly2.GetType("PrintReflectionTest.PrintTest");
//使用指定类创建实例 CreateInstance(type,true) 可以突破类的限制(可以访问到非公开的)
//获取指定有参构造函数
var oPrintTest = Activator.CreateInstance(type, true);
//var oResult = Activator.CreateInstance(type, new object[] { "张三" });

//获取指定公开静态有参函数
var printFunc = type.GetMethod("GetInstance", BindingFlags.Static | BindingFlags.Public);
printFunc.Invoke(oPrintTest, new object[] { "张三" });

//普通方法无参方法
var myTestPrint1 = type.GetMethod("MyTestPrint1");
myTestPrint1.Invoke(oPrintTest, null);

//普通方法一个参数
var myTestPrint2 = type.GetMethod("MyTestPrint2", new Type[] { typeof(string) });
myTestPrint2.Invoke(oPrintTest, new object[] { "普通方法一个参数" });

//普通方法myTestPrint2重载两个参数
var _myTestPrint2 = type.GetMethod("MyTestPrint2", new Type[] { typeof(string), typeof(DateTime) });
//这个也可以获取
//var _myTestPrint2 = type.GetMethod("MyTestPrint2", new Type[] { typeof(string), typeof(DateTime) });
_myTestPrint2.Invoke(oPrintTest, new object[] { "普通方法一个参数", DateTime.Now });

//私有普通方法myTestPrint3无参 需要  BindingFlags.NonPublic|BindingFlags.Instance
var myTestPrint3 = type.GetMethod("MyTestPrint3", BindingFlags.NonPublic | BindingFlags.Instance);
myTestPrint3.Invoke(oPrintTest, new object[] { });

//内部方法 需要  BindingFlags.NonPublic|BindingFlags.Instance
var myTestPrint4 = type.GetMethod("MyTestPrint4", BindingFlags.NonPublic | BindingFlags.Instance);
myTestPrint4.Invoke(oPrintTest, new object[] { });

//获取指定方法
var getResult = type.GetMethod("GetResult");
//给泛型方法指定类型
var strfunc = getResult.MakeGenericMethod(new Type[] { typeof(string) });
strfunc.Invoke(oPrintTest, new object[] { "给泛型方法指定类型(string)" });

调用反射内泛型方法

//获取指定类 "`3" 指三个类型参数
var _type = printReflectionTestAssembly2.GetType("PrintReflectionTest.GenericClass`3");
//获取指定的类,指定泛型
_type = _type.MakeGenericType(new Type[] { typeof(string), typeof(string), typeof(int) });
//把这个泛型类实例化
var oGenericClass = Activator.CreateInstance(_type);
//获取指定方法
var func = _type.GetMethod("Show");
//调用泛型方法
func.Invoke(oGenericClass, new object[] { "张三", "男", 19 });

//获取指定类 
_type = printReflectionTestAssembly2.GetType("PrintReflectionTest.GenericMethod");
//方法需要传入三个泛型
func = _type.GetMethod("Show");
func = func.MakeGenericMethod(new Type[] { typeof(string), typeof(string), typeof(int) });
//创建实例
var oGenericMethod = Activator.CreateInstance(_type);
//调用泛型方法
func.Invoke(oGenericMethod, new object[] { "张三", "男", 19 });

//类需要一个泛型
_type = printReflectionTestAssembly2.GetType("PrintReflectionTest.GenericDouble`1");
//给类指定一个泛型
_type = _type.MakeGenericType(new Type[] { typeof(string) });
//创建实例
var oGenericDouble = Activator.CreateInstance(_type);
//方法需要三个泛型,但是类指定了一个,所以方法需要两个
func = _type.GetMethod("Show").MakeGenericMethod(new Type[] { typeof(string), typeof(int) });
func.Invoke(oGenericDouble, new object[] { "张三", "男", 19 });
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Izrj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值