網站首頁 編程語言 正文
一、創建屬性
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor, AllowMultiple = true, Inherited = true)]
//AttributeTargets:屬性應用到的目標類型。AllowMultiple:是否允許一個元素應用多個此屬性。Inherited:屬性能否有派生類繼承。
public class CodeStatusAttribute : Attribute
{
private string status;
public CodeStatusAttribute(string status)//構造函數為位置參數
{
this.status = status;
}
public string Tester { set; get; }//屬性和公共字段為命名參數
public string Coder { set; get; }
public override string ToString()
{
return status;
}
}
二、應用屬性
//1、使用單個屬性
[CodeStatus("a版")]
public class Tringe
{ }
//2、使用多個屬性
[CodeStatus("b版", Coder = "小李")]
[CodeStatus("b版", Coder = "小王")]
//也可以[CodeStatus("aa",Coder="小李"),CodeStatus("aa",Coder="小王")]
public class Square
{ }
//3、使用位置參數和命名參數
//type表示此屬性與什么元素關聯,可能有:assembly,field,method,param,property,return,moudule,event,type等。。
[type: CodeStatus("最終版", Coder = "小李", Tester = "老李")]
public class Circle
{
[CodeStatus("最終版", Coder = "小李", Tester = "老李")]
public Circle()
{
}
}
三、反射屬性
//1、獲取類上的屬性。
Type t = typeof(Circle);
Attribute[] attArr = Attribute.GetCustomAttributes(t, typeof(CodeStatusAttribute));
//或
object[] attArr1 = t.GetCustomAttributes(typeof(CodeStatusAttribute), true);
//2、獲取成員上屬性
Attribute[] attArr3 = t.GetConstructors()[0].GetCustomAttributes().ToArray();//構造函數,獲取字段GetField("..")
//3、遍歷
foreach (Attribute attr in attArr3)
{
CodeStatusAttribute item = (CodeStatusAttribute)attr;
Console.Write(item.ToString() + item.Coder + item.Tester);
}
四、Net內置屬性
[Condeitonal] //條件控制
[Obsolete] //廢棄屬性
[Serializable]//可序列化屬性
[AssemblyDelaySign] //程序集延遲簽名
原文鏈接:https://www.cnblogs.com/springsnow/p/9433923.html
相關推薦
- 2023-01-04 利用Python對哥德巴赫猜想進行檢驗和推理_python
- 2022-02-18 spring常見錯誤:Error creating bean with name ‘xxx‘
- 2022-07-28 Go語言反射reflect.Value實現方法的調用_Golang
- 2022-09-05 C語言模擬實現庫函數詳解_C 語言
- 2023-04-02 pytorch?transform數據處理轉c++問題_python
- 2022-04-18 使用numpy對數組求平均時如何忽略nan值_python
- 2022-11-05 React+CSS?實現繪制橫向柱狀圖_React
- 2022-04-30 利用Python生成Excel炫酷圖表_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同步修改后的遠程分支