首页 > 其他 > 详细

【转载】#470 Define Your Own Custom Attribute

时间:2014-03-21 04:10:23      阅读:458      评论:0      收藏:0      [点我收藏+]

You can use predefined attributes to attach metadata to type members.

You can also define a custom attribute by creating a new class inheriting from System.Attribute. The class name must end in "Attribute". You typically define a conostructor that takes arguments that consist of the metadata that you want to attach to the type membere.

bubuko.com,布布扣
 1 /// <summary>
 2 /// Attach to a class method to indicate kg of methane that is
 3 /// output when calling the method.
 4 /// </summary>
 5 public class MethaneFootprintAttribute : Attribute
 6 {
 7     public double kgMethane;
 8  
 9     public MethaneFootprintAttribute(int kg)
10     {
11         kgMethane = kg;
12     }
13 }
bubuko.com,布布扣

You can use the new attribute to attach metadata to individual type members. You use the name of the new class, without the trailing "Attribute".

bubuko.com,布布扣
 1 [MethaneFootprint(45)]
 2 public void FeedCowInBarn()
 3 {
 4     Console.WriteLine("Cow eats slop in dim confines of barn");
 5 }
 6  
 7 [MethaneFootprint(29)]
 8 public void LetGrazeOutside()
 9 {
10     Console.WriteLine("Cow enjoys grazing and ends up healthier");
11 }
bubuko.com,布布扣

原文地址:#470 Define Your Own Custom Attribute

【转载】#470 Define Your Own Custom Attribute,布布扣,bubuko.com

【转载】#470 Define Your Own Custom Attribute

原文:http://www.cnblogs.com/yuthreestone/p/3614676.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!