網站首頁 編程語言 正文
一、使用方法
查找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
相關推薦
- 2022-09-18 Go實現文件分片上傳_Golang
- 2022-10-12 python繪制發散型柱狀圖+誤差陰影時間序列圖+雙坐標系時間序列圖+繪制金字塔圖_python
- 2022-08-03 Flutter實現切換應用時隱藏應用預覽_Android
- 2023-04-06 python判斷列表為空的三種方法總結_python
- 2022-06-01 如何利用Python將字典轉為成員變量_python
- 2022-12-22 一文帶你深入了解Go語言中切片的奧秘_Golang
- 2022-05-21 Python中三種花式打印的示例詳解_python
- 2022-06-01 Python實現xml格式轉txt格式的示例代碼_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支