特性(Attribute)的用处

Attribute特性,可以往程序集中写入一些元数据。微软类库中自带很多 Attribute类,某些时候,需要对类标注一下 Attribute。那么自定义 Attribute的用处是什么的,或者说,什么时候我们需要自定义 Attribute。

在我看来, 自定义Attribute没啥用。常规的项目开发中,我都可以用变通的方式去实现。当然硬是要为了用而用也可以,就是写一些信息到程序集中,然后在调用的时候在读取这些信息。采用反射的技术,但这不是多此一举影响性能么,直接把信息传给类不更好吗?

所以不清楚何时何地必须要Attribute。下面的例子,就是,用到反射读取Attribute的信息。
  using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Dynamic ;

namespace ConsoleApplication1
{
    class Program
    {

        // create custom attribute to be assigned to class members
        [AttributeUsage (AttributeTargets . Class |
        AttributeTargets . Constructor |
        AttributeTargets . Field |
        AttributeTargets . Method |
        AttributeTargets . Property,
        AllowMultiple = true ) ]
        public class BugFixAttribute : System . Attribute
        {
            // attribute constructor for positional parameters
            public BugFixAttribute
            (
            int bugID,
            string programmer,
            string date
            )
            {
                this . BugID = bugID ;
                this . Programmer = programmer ;
                this . Date = date ;
            }
            // accessors
            public int BugID { get ; private set ; }
            public string Date { get ; private set ; }
            public string Programmer { get ; private set ; }
            // property for named parameter
            public string Comment { get ; set ; }
        }
        // ********* assign the attributes to the class ********


        [BugFixAttribute ( 121, "Jesse Liberty", "01/03/08" ) ]
        [BugFixAttribute ( 107, "Jesse Liberty", "01/04/08",
        Comment = "Fixed off by one errors" ) ]
        public class MyMath
        {
            public double DoFunc1 ( double param1 )
            {
                return param1 + DoFunc2 (param1 ) ;
            }
            public double DoFunc2 ( double param1 )
            {
                return param1 / 3 ;

            }
        }

        public class Tester
        {
            static void Main ( string [ ] args )
            {
                MyMath mm = new MyMath ( ) ;
                Console . WriteLine ( "Calling DoFunc(7). Result: {0}",
                mm . DoFunc1 ( 7 ) ) ;

                // get the member information and use it to
                // retrieve the custom attributes
                System.Reflection . MemberInfo inf = typeof (MyMath ) ;
                object [ ] attributes ;
                attributes = inf . GetCustomAttributes (
                typeof (BugFixAttribute ), false ) ;
                // iterate through the attributes, retrieving the
                // properties
                foreach ( Object attribute in attributes )
                {
                    BugFixAttribute bfa = (BugFixAttribute )attribute ;
                    Console . WriteLine ( "\nBugID: {0}", bfa . BugID ) ;
                    Console . WriteLine ( "Programmer: {0}", bfa . Programmer ) ;
                    Console . WriteLine ( "Date: {0}", bfa . Date ) ;
                    Console . WriteLine ( "Comment: {0}", bfa . Comment ) ;
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值