设计模式体现的是一种思想,而思想是指导行为的一切------------开发智能要比机械性的生产商品要简单的多./我要从南走到北,从1到2^10/-----------------Life is a pure flame, and we live by an invisible within us.

15.Attribute 类[摘自MSDN]

上一篇 / 下一篇  2008-05-05 22:07:16 / 个人分类:::NET Framework 类库::

表示自定义属性的基类。

命名空间: System
-v:Y4Lm dKf m"Q;_ RqS0程序集: mscorlib(在 mscorlib.dll 中)

Visual Basic(声明)
<SerializableAttribute> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.All, Inherited :=True, AllowMultiple :=False)> _PublicMustInheritClassAttribute _Implements_Attribute
Visual Basic(用法)
DiminstanceAsAttribute
C#
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets.All, Inherited =true, AllowMultiple =false)]publicabstractclassAttribute : _Attribute
Visual C++
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType::None)]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets::All, Inherited =true, AllowMultiple =false)]publicrefclassAttribute abstract : _Attribute
J#
/** @attribute SerializableAttribute *//** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) *//** @attribute ComVisibleAttribute(true) *//** @attribute AttributeUsageAttribute(AttributeTargets.All, Inherited =true, AllowMultiple =false) */publicabstractclassAttribute implements _Attribute
JScript
publicabstractclassAttribute implements _Attribute

Attribute类将预定义的系统信息或用户定义的自定义信息与目标元素相关联。目标元素可以是程序集、类、构造函数、委托、枚举、事件、字段、接口、方法、可移植可执行文件模块、参数、属性 (Property)、返回值、结构或其他属性 (Attribute)。

属性所提供的信息也称为元数据。元数据可由应用程序在运行时进行检查以控制程序处理数据的方式,也可以由外部工具在运行前检查以控制应用程序处理或维护自身的方式。例如,.NET Framework 预定义属性类型并使用属性类型控制运行时行为,某些编程语言使用属性类型表示 .NET Framework 公共类型系统不直接支持的语言功能。

所有属性类型都直接或间接地从Attribute类派生。属性可应用于任何目标元素;多个属性可应用于同一目标元素;并且属性可由从目标元素派生的元素继承。使用AttributeTargets类可以指定属性所应用到的目标元素。

Attribute类提供检索和测试自定义属性的简便方法。有关使用属性的更多信息,请参见利用属性扩展元数据

下面的代码示例演示Attribute的用法。

C#
usingSystem;
|d{'d&T Z.z0usingSystem.Reflection;ITPUB个人空间 q(x!}s#A{#q
ITPUB个人空间.T/nU1sT$L5o'K
namespaceCustomAttrCS {
o/WL{$_0   // An enumeration of animals. Start at 1 (0 = uninitialized).ITPUB个人空间Fzf|#g+X2A nwa8@}
   publicenum Animal {
h_1M`,FZK0       // Pets.ITPUB个人空间4sUH;l8K
        Dog = 1,
&E'Ab7Uykb@.| C0        Cat,
O$dTe8U ]D7M"Q0        Bird,
{(JYCHX L7x%m }^0    }
slaG/hKZ1c0ITPUB个人空间*H2xT#]8P7kr
   // A custom attribute to allow a target to have a pet.
3b c'~ u2qrz}.|N0   publicclassAnimalTypeAttribute : Attribute {ITPUB个人空间t S+@P%_4t3lk
       // The constructor is called when the attribute is set.
*W Z)a [9mN4t0       publicAnimalTypeAttribute(Animal pet) {
dDvie b0            thePet = pet;
c!K-] _\&[eY0        }ITPUB个人空间d*k{h*qpk
ITPUB个人空间+D~:T[8g/w]`z7]jZ
       // Keep a variable internally ...ITPUB个人空间 Q Vu]xO E
       protectedAnimal thePet;ITPUB个人空间|GRJ1H#_"LWY
ITPUB个人空间Z)BK n,Gro ro3G,D
       // .. and show a copy to the outside world.
g0N%dF| c3|0       publicAnimal Pet {ITPUB个人空间y#kE s C Y#JrYA
           get{returnthePet; }ITPUB个人空间be'u@z"{RDw5K
           set{ thePet = Pet; }
$b0z6c&a.I*wD_0        }
k;|DC\-|0    }
2w,n7f)\7|/x/~ N0
9jQ$j.oX0   // A test class where each method has its own pet.
2@"|mc(M0   classAnimalTypeTestClass {
Ia RXeah0        [AnimalType(Animal.Dog)]ITPUB个人空间}t/` e ?!Zd&p
       publicvoidDogMethod() {}
Zs4oN\*c)?q0
~,b5l,L JMf r!q0        [AnimalType(Animal.Cat)]ITPUB个人空间 Th,`#y6RA
       publicvoidCatMethod() {}
:H(OG+T-Y%b0Q k0ITPUB个人空间4UVZG,M\,y
        [AnimalType(Animal.Bird)]ITPUB个人空间v"c Z1mk
       publicvoidBirdMethod() {}
K%tt$p0YJ?0N0    }
8{0LBIjj c0
#i q7IYpA0   classDemoClass {
Oma\@L O-}0       staticvoidMain(string[] args) {ITPUB个人空间5l P~3?q(L2Nk@ Q5_
            AnimalTypeTestClass testClass =newAnimalTypeTestClass(); Type type = testClass.GetType();// Iterate through all the methods of the class.
.^Ol iT cmN)}0           foreach(MethodInfo mInfointype.GetMethods()) {
W T%]P ?(n.R'VE0               // Iterate through all the Attributes for each method.ITPUB个人空间 T&D"I#^:L+] Ry"z8b
               foreach(Attribute attrinAttribute.GetCustomAttributes(mInfo)) {// Check for the AnimalType attribute.
er8}N4k;u*lY"ZE0                   if(attr.GetType() == typeof(AnimalTypeAttribute))
hC@/L#|!e&b0                        Console.WriteLine(ITPUB个人空间X)@r0f0TBh8T
                           "Method {0} has a pet {1} attribute.",ITPUB个人空间WgF2m xA&L
                            mInfo.Name, ((AnimalTypeAttribute)attr).Pet);ITPUB个人空间,O!nnOMU0M
                }
:fp{q:V"b0
#jIVF~*Hv q0            }
0diUn-c Qz#n j{G0        }ITPUB个人空间|5r'h@ vX
    }ITPUB个人空间'sP.o r~ _dY ef9U
}
a k|[]*PQ0O0^0ITPUB个人空间H ] s8tSF
/*
-@y v ]wM?.r0* Output:ITPUB个人空间a.{\/Eq4uM
* Method DogMethod has a pet Dog attribute.ITPUB个人空间/t r'@#z"o,sK
* Method CatMethod has a pet Cat attribute.ITPUB个人空间5s ]$\D V'Q
* Method BirdMethod has a pet Bird attribute.ITPUB个人空间 ?3\w'lIi |1[i
*/

6CDH^+xK$K0

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-10-13  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 393
  • 日志数: 17
  • 建立时间: 2008-04-10
  • 更新时间: 2008-05-14

RSS订阅

Open Toolbar