網站首頁 編程語言 正文
反射允許我們在編譯期或運行時獲取程序集的元數據,通過反射可以做到:
● 創建類型的實例
● 觸發方法
● 獲取屬性、字段信息
● 延遲綁定
......
如果在編譯期使用反射,可通過如下2種方式獲取程序集Type類型:
- 1、Type類的靜態方法
Type type = Type.GetType("somenamespace.someclass");
- 2、通過typeof
Type type = typeof(someclass);
如果在運行時使用反射,通過運行時的Assembly實例方法獲取Type類型:
Type type = asm.GetType("somenamespace.someclass");
獲取反射信息
有這樣的一個類:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public float Score { get; set; }
public Student()
{
this.Id = -1;
this.Name = string.Empty;
this.Score = 0;
}
public Student(int id, string name, float score)
{
this.Id = id;
this.Name = name;
this.Score = score;
}
public string DisplayName(string name)
{
return string.Format("學生姓名:{0}", name);
}
public void ShowScore()
{
Console.WriteLine("學生分數是:" + this.Score);
}
}
通過如下獲取反射信息:
static void Main(string[] args)
{
Type type = Type.GetType("ConsoleApplication1.Student");
//Type type = typeof (Student);
Console.WriteLine(type.FullName);
Console.WriteLine(type.Namespace);
Console.WriteLine(type.Name);
//獲取屬性
PropertyInfo[] props = type.GetProperties();
foreach (PropertyInfo prop in props)
{
Console.WriteLine(prop.Name);
}
//獲取方法
MethodInfo[] methods = type.GetMethods();
foreach (MethodInfo method in methods)
{
Console.WriteLine(method.ReturnType.Name);
Console.WriteLine(method.Name);
}
Console.ReadKey();
}
?延遲綁定
在通常情況下,為對象實例賦值是發生在編譯期,如下:
Student stu = new Student();
stu.Name = "somename";
而"延遲綁定",為對象實例賦值或調用其方法是發生在運行時,需要獲取在運行時的程序集、Type類型、方法、屬性等。
//獲取運行時的程序集
Assembly asm = Assembly.GetExecutingAssembly();
//獲取運行時的Type類型
Type type = asm.GetType("ConsoleApplication1.Student");
//獲取運行時的對象實例
object stu = Activator.CreateInstance(type);
//獲取運行時指定方法
MethodInfo method = type.GetMethod("DisplayName");
object[] parameters = new object[1];
parameters[0] = "Darren";
//觸發運行時的方法
string result = (string)method.Invoke(stu, parameters);
Console.WriteLine(result);
Console.ReadKey();
原文鏈接:https://www.cnblogs.com/darrenji/p/3817999.html
相關推薦
- 2024-01-30 關閉idea時出現waiting for process detach解決辦法
- 2022-11-01 如何將asp.net?core程序部署到Linux服務器_實用技巧
- 2022-05-17 ubuntu Could NOT find CURL (missing: CURL_LIBRARY
- 2021-12-01 C語言多維數組數據結構的實現詳解_C 語言
- 2022-07-04 PyTorch深度學習LSTM從input輸入到Linear輸出_python
- 2022-11-28 深入了解C++函數重載解析策略_C 語言
- 2022-09-16 記一次SQL優化的實戰記錄_數據庫其它
- 2022-12-22 Flutter組件開發過程完整講解_Android
- 最近更新
-
- 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同步修改后的遠程分支