C#反射调用dll文件中的方法操作泛型与属性字段
作者:農碼一生 时间:2022-09-15 03:24:04
一、使用方法
查找DLL文件,
通过Reflection反射类库里的各种方法来操作dll文件
二、步骤
加载DLL文件
Assembly assembly1 = Assembly.Load("SqlServerDB");//方式一:这个DLL文件要在启动项目下
string filePath = Environment.CurrentDirectory + "";
Assembly assembly2 = Assembly.LoadFile(filePath + @"\SqlServerDB.dll");//方式二:完整路径
Assembly assembly3 = Assembly.LoadFrom(filePath + @"\SqlServerDB.dll");//方式三:完整路径
Assembly assembly4 = Assembly.LoadFrom(@"SqlServerDB.dll");//方式三:完整路径
获取指定类型
foreach (var item in assembly4.GetTypes())//查找所有的类型,就是有多少个类
{
Console.WriteLine(item.Name);
}
获取构造函数
Type type = assembly4.GetType("SqlServerDB.ReflectionTest");//在ReflectionTest类中调用
foreach (var ctor in type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
Console.WriteLine($"构造方法:{ctor.Name}");
foreach (var param in ctor.GetParameters())
{
Console.WriteLine($"构造方法的参数:{param.ParameterType}");
}
}
//【3】实例化
//ReflectionTest reflectionTest = new ReflectionTest();//这种实例化是知道具体类型--静态
//object objTest = Activator.CreateInstance(type);//动态实例化--调用我们的构造方法
object objTest1 = Activator.CreateInstance(type, new object[] { "string" });//动态实例化--调用我们的有参数构造方法
//调用私有构造函数
//ReflectionTest reflectionTest = new ReflectionTest(); //普通调用
object objTest2 = Activator.CreateInstance(type, true);
调用非构造方法
object objTest2 = Activator.CreateInstance(type, true);
//调用普通方法
ReflectionTest reflectionTest = objTest2 as ReflectionTest;//as转换的好处,它不报错,类型不对的话就返回null
reflectionTest.Show1();
//调用私有方法
var method = type.GetMethod("Show2", BindingFlags.Instance | BindingFlags.NonPublic);
method.Invoke(objTest2, new object[] { });
调用泛型方法
//泛型无参数
var method3 = type.GetMethod("Show3");//查找指定方法
var genericMethod = method3.MakeGenericMethod(new Type[] { typeof(int) });//指定泛型参数类型T
genericMethod.Invoke(objTest2, new object[] { });
//泛型有参数
var method4 = type.GetMethod("Show4");//查找指定方法
var genericMethod4 = method4.MakeGenericMethod(new Type[] { typeof(string) });//指定泛型参数类型T
genericMethod4.Invoke(objTest2, new object[] { 123, "泛型string参数" });
反射测试类
位于SqlServerDB.dll中的ReflectionTest.cs文件中
/// <summary>
/// 反射测试类
/// </summary>
public class ReflectionTest
{
//私有构造函数
private ReflectionTest()
{
Console.WriteLine("这是私有无参数构造方法");
}
//普通构造函数
//public ReflectionTest()
//{
// Console.WriteLine("这是无参数构造方法");
//}
public ReflectionTest(string name)
{
Console.WriteLine($"这是有参数构造方法+参数值是:{name}");
}
public void Show1()
{
Console.WriteLine("调用普通方法", this.GetType());
}
private void Show2()
{
Console.WriteLine("调用私有方法",this.GetType());
}
public void Show3<T>()
{
Console.WriteLine("调用无参数泛型方法", this.GetType());
}
public void Show4<T>(int id,string name)
{
Console.WriteLine($"调用有参数泛型方法,参数是{id},{name}", this.GetType());
}
}
操作泛型类和泛型方法
加载DLL文件
Assembly assembly = Assembly.LoadFrom(@"SqlServerDB.dll");
获取指定类型
Type type = assembly.GetType("SqlServerDB.GenericClass`2").MakeGenericType(typeof(int), typeof(string));//一定给定具体类型参数
调用泛型方法
object objTest2 = Activator.CreateInstance(type);
var method = type.GetMethod("GenericMethod").MakeGenericMethod(typeof(int));
method.Invoke(objTest2, new object[] { });
反射测试类
位于SqlServerDB.dll中的GenericClass.cs文件中
public class GenericClass<T,W>
{
public void GenericMethod<TType>()
{
Console.WriteLine("泛型类调用+泛型方法");
}
}
操作类属性字段
加载DLL文件
Assembly assembly2 = Assembly.LoadFrom("SqlServerDB.dll");
获取指定类型
Type type2 = assembly2.GetType("SqlServerDB.PropertyClass");
调用泛型方法
object obj = Activator.CreateInstance(type2);
foreach (var property in type2.GetProperties())
{
Console.WriteLine(property.Name);
//给属性设置值
if (property.Name.Equals("Id"))
{
property.SetValue(obj, 1);
}
else if (property.Name.Equals("Name"))
{
property.SetValue(obj, "学习编程");
}
else if (property.Name.Equals("Phone"))
{
property.SetValue(obj, "123459789");
}
//获取属性值
Console.WriteLine(property.GetValue(obj));
}
反射测试类
位于SqlServerDB.dll中的PropertyClass.cs文件中
public class PropertyClass
{
public int Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
}
来源:https://www.cnblogs.com/wml-it/p/16055421.html
标签:C#,反射,调用,dll,文件,操作,泛型,属性,字段
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#通过流写入一行数据到文件的方法
2023-08-22 13:04:08
详解Android Webview加载网页时发送HTTP头信息
2023-11-16 02:32:34
详解Java实现缓存(LRU,FIFO)
2022-04-24 13:35:26
![](https://img.aspxhome.com/file/2023/1/64101_0s.png)
Java中Range函数的简单介绍
2023-10-18 05:33:35
![](https://img.aspxhome.com/file/2023/0/66970_0s.png)
C#中事件的动态调用实现方法
2022-08-29 03:27:28
android动态设置app当前运行语言的方法
2022-02-21 01:33:37
![](https://img.aspxhome.com/file/2023/1/100611_0s.jpg)
JAVA面试题之Forward与Redirect的区别详解
2023-11-25 02:27:15
![](https://img.aspxhome.com/file/2023/2/59912_0s.png)
详解SpringBoot修改启动端口server.port的四种方式
2022-03-10 20:28:48
C#实现百分比转小数的方法
2021-09-09 00:53:53
Java提取2个集合中的相同和不同元素代码示例
2023-11-28 05:48:41
Spring Data环境搭建实现过程解析
2022-02-26 20:13:38
java排序算法之冒泡排序
2023-04-05 21:03:42
![](https://img.aspxhome.com/file/2023/7/72637_0s.gif)
Android开发中WebView的简单使用小结
2022-09-11 00:22:43
![](https://img.aspxhome.com/file/2023/2/116012_0s.jpg)
androidQ sd卡权限使用详解
2021-09-27 17:38:51
浅谈Java中注解Annotation的定义、使用、解析
2021-07-29 04:07:52
关于C#10 新特性 Lambda 优化
2022-05-31 08:21:16
IDEA 中使用 Hudi的示例代码
2021-08-27 21:51:04
![](https://img.aspxhome.com/file/2023/6/76236_0s.jpg)
一文搞懂Spring中的注解与反射
2023-11-11 11:38:18
![](https://img.aspxhome.com/file/2023/8/58758_0s.jpg)
C#接口归纳总结实例详解
2021-08-18 16:55:54
详解用Eclipse如何创建Web项目
2023-11-11 05:41:01
![](https://img.aspxhome.com/file/2023/3/59393_0s.png)