C 拾遺系列 7 自定義屬性

2021-09-22 08:59:46 字數 1098 閱讀 2630

1 .描述

屬性提供功能強大的方法以將宣告資訊與 c# **(型別、方法、屬性等)相關聯。屬性與程式實體關聯後,即可在執行時使用名為「反射」的技術查詢屬性。

屬性以兩種形式出現:

2. 示例**:

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace nettest/*

obsolete 屬性將某個程式實體標記為乙個建議不再使用的實體。每次使用被標記為已過時的實體時,

隨後將生成警告或錯誤,這取決於屬性是如何配置的,第二個引數是true時,編譯時顯示錯誤

*/[obsolete("please use aonther method,this is obsolate",true)]

public

void testobsolate()

private

static

void printauthorinfo(system.type t)

", t);

system.attribute attrs = system.attribute.getcustomattributes(t);  // reflection

foreach (system.attribute attr in attrs)

, version ", a.name, a.version);}}

}//應用自定義屬性

[author("jack",version=1.0)]

[author("tj",version=2.0)]

class

customattribute

}//自定義的屬性,整合屬性類

[system.attributeusage(attributetargets.class|attributetargets.struct,allowmultiple=true)]

class

author : system.attribute

public

string name}}

}}王德水

C 拾遺系列 7 自定義屬性

1 描述 屬性提供功能強大的方法以將宣告資訊與 c 型別 方法 屬性等 相關聯。屬性與程式實體關聯後,即可在執行時使用名為 反射 的技術查詢屬性。屬性以兩種形式出現 2.示例 using system using system.collections.generic using system.lin...

C 拾遺系列 3 建構函式

1.主要演示建構函式的繼承 2.靜態建構函式 3.示例 using system using system.collections.generic using system.linq using system.text description 建構函式測試 namespace nettest pub...

C 拾遺系列 6 迭代器

1.示例 using system using system.collections.generic using system.linq using system.text using system.collections namespace nettest public class testite...