. net中的属性attribute,有的地方也叫特性了,通常是用来修饰类,当然还可以修接口、方法、字段。
加了attribute属性的地方,可以通常过反射的方式来取得特性值。从而进行一些过滤操作。
用户可以通过继承System.Attribute类来实现自定义的属性。在使用的地方使用反射机制来使用。
以下是一些用例代码:
下面是调用的示例:
结果:
Method DogMethod has a pet Dog attribute.
Type is Tom.Test.AnimalTypeAttribute
Method CatMethod has a pet Cat attribute.
Type is Tom.Test.AnimalTypeAttribute
Method BirdMethod has a pet Bird attribute.
Type is Tom.Test.AnimalTypeAttribute
本文介绍C#中Attribute特性的使用方法,包括自定义Attribute的创建与应用,通过反射获取特性值,并展示了具体的代码实例。
959

被折叠的 条评论
为什么被折叠?



