C#反射特性(二)

鄙人初学反射特性动手练习了一下:

分三步:首先自定义MeetingRecordAttribute类;

           然后在其它的类中引用使用该特性;

          最后访问该特性的基本信息;

(-)具体的源代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConAttribute
{
    //AllowMultiple=true表示这个特性可以多次应用到同一应用程序元素上,Interited=true表示子类可以具有这个特性
    [AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Property,AllowMultiple=true,Inherited=false)]
    class MeetingRecordAttribute:Attribute
    {

 

        /// <summary>
        /// 记录会议的主要内容
        /// /// </summary>
        ///
      
        private string description;
        public string Description//对于位置参数,通常只提供get访问器

        {
            get { return description; }          
        }
        /// <summary>
        /// 记录开会的日期
        /// </summary>
        private DateTime dateTime;
        public DateTime DateTime// 对于位置参数,通常只提供get访问器

        {
            get { return dateTime; }          
        }
        /// <summary>
        /// 出席的人数
        /// </summary>
        private int numbers;
        public int Numbers// 对于位置参数,通常只提供get访问器
        {
            get { return numbers; }
           
        }

 

        /// <summary>
        ///出席的男女比例
        /// </summary>
        private double scale;
        public double Scale// 对于位置参数,通常只提供get访问器

        {
            get { return scale; }         
        }


        //在特性中的属性也成为"命名参数";
        public string HostName
        {
            get;
            set;
        }

        // 构造函数,构造函数的参数在特性中也称为“位置参数”。
        public MeetingRecordAttribute(string description, string dt, int nums, double scale):base()
        {
            this.description = description;
            this.dateTime =Convert.ToDateTime(dt);
            this.numbers = nums;
            this.scale = scale;
        }
        public override string ToString()
        {
            return string.Format("XXXXX:{0}", this.dateTime.ToShortDateString());
        }

        public static void ShowMessage(string msg)
        {

            Console.WriteLine(msg);
        }
    }
}

 

(二)使用上面自定义的特性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConAttribute
{
    [MeetingRecord("XXXXX1.0营销策略培训", "2013-3-26", 8, 0.4, HostName = "XXX总经理")]
    [MeetingRecord("XXXXX2.0英语学习培训", "2013-3-27", 8, 0.4, HostName = "XXX人事部总经理")]
    [MeetingRecord("XXXXX3.0C#核心开发框架代码培训","2013-3-28",8,0.4)]
    class Meeting
    {
        #region 会议字段的定义;
        private string name;
        private string description;
        private string spearker;
        #endregion

        #region 属性的定义
        public string Name
        {

        get { return name; }
        set { name = value; }
        }

        public string Description
        {
        get { return description; }
        set { description = value; }

        }
        public string Speaker
        {
        get { return spearker; }
        set { spearker = value; }
        }
        #endregion


        #region 成员方法的定义
        public void ShowMessage(string msg)
        {

            Console.WriteLine(msg);
        }


        public void StartMeeting()
        {

        }

        public void interaction()
        {

        }


  
        #endregion


        }

}

 

(三)访问第二步使用的特性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConAttribute
{
    class TestMeetingAttribute
    {
        public static void Main(string[] args)
        {

            Console.WindowWidth = 200;
            Console.WindowHeight = 40;
           
            //Type t = Type.GetType("ConAttribute.Meeting");//这行代码与下一行的功能一样
            Type t = typeof(Meeting);
                Console.WriteLine("{0}具有的的特性信息如下:", t);
                //object[] meetRecords = t.GetCustomAttributes(Type.GetType("ConAttribute.MeetingRecordAttribute"), false);//这行与下一行的功能一样
                object[] meetRecords = t.GetCustomAttributes(typeof(MeetingRecordAttribute), false);

                foreach (var item in meetRecords)
                {
                    MeetingRecordAttribute m = item as MeetingRecordAttribute;
                    Console.Write("会议描述:" + m.Description + " 开会日期:" + m.DateTime + " 参加人数:" + m.Numbers + " 男女比例:" + m.Scale);
                    if (!string.IsNullOrEmpty(m.HostName))
                    {
                        Console.Write("  主持人:" + m.HostName);
                    }
                    Console.WriteLine();
                }

                Console.Read();
          
         
        }
    }
}

 

(四)输出结果如下图:

 

posted @ 2013-03-28 18:45  Predator  阅读(157)  评论(0编辑  收藏  举报