網(wǎng)站首頁 編程語言 正文
反射允許我們在編譯期或運行時獲取程序集的元數(shù)據(jù),通過反射可以做到:
● 創(chuàng)建類型的實例
● 觸發(fā)方法
● 獲取屬性、字段信息
● 延遲綁定
......
如果在編譯期使用反射,可通過如下2種方式獲取程序集Type類型:
- 1、Type類的靜態(tài)方法
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("學生分數(shù)是:" + 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();
}
?延遲綁定
在通常情況下,為對象實例賦值是發(fā)生在編譯期,如下:
Student stu = new Student();
stu.Name = "somename";
而"延遲綁定",為對象實例賦值或調(diào)用其方法是發(fā)生在運行時,需要獲取在運行時的程序集、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";
//觸發(fā)運行時的方法
string result = (string)method.Invoke(stu, parameters);
Console.WriteLine(result);
Console.ReadKey();
原文鏈接:https://www.cnblogs.com/darrenji/p/3817999.html
相關推薦
- 2021-12-13 C語言數(shù)據(jù)結構與算法之圖的遍歷(一)_C 語言
- 2022-12-02 Jetpack?Compose自定義動畫與Animatable詳解_Android
- 2022-11-01 Python如何使用qrcode生成指定內(nèi)容的二維碼并在GUI界面顯示_python
- 2022-10-04 python編寫一個GUI倒計時器_python
- 2023-01-12 利用C#實現(xiàn)進程管理器_C#教程
- 2022-03-24 .Net?Core服務治理Consul自動擴展和服務調(diào)用_自學過程
- 2022-04-12 Taro打包Android?apk過程詳解_Android
- 2022-11-05 在jupyter?notebook中使用pytorch的方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支