C#attribute属性の例


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
    // student attribute student attribute 
    [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
    class StudentAttribute : Attribute
    {
        public StudentAttribute() { } // 
        private string name;
        private int age;
        private string privilege;
        private string description;

        /// <summary>
        ///  
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        /// <summary>
        ///  
        /// </summary>
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        /// <summary>
        ///  
        /// </summary>
        public string Privilege
        {
            get { return privilege; }
            set { privilege = value; }
        }
        /// <summary>
        ///  
        /// </summary>
        public string Description
        {
            get { return description; }
            set { description = value; }
        }
    }
    [Student(Name = "Locus", Age = 29)]
    class Student
    { 

       //private 
        [Student(Description = "studentName")]
        private string Name;

        private string stuClass;
        //        
        [Student(Description = "studentClass", Name = "studentClass1")]
        public string StuClass
        {
            get { return stuClass; }
            set { stuClass = value; }
        }

        [Student(Description = "studentRead")] // 
        public void ReadBook()
        {
            Console.WriteLine("Student read book");
        }
    }

    class Program
    {
        static object[] objs;
        static StudentAttribute sat;

        static void Main(string[] args)
        {
           
            var attrs = typeof(Student).GetCustomAttributes(typeof(StudentAttribute), true);
            Console.WriteLine((attrs[0] as StudentAttribute).Age);
            Console.WriteLine((attrs[0] as StudentAttribute).Name);
            
            
            PropertyInfo[] propArray = null;
            MethodInfo[]  methodArray=null;

            propArray = typeof(Student).GetProperties();
            methodArray=typeof(Student).GetMethods();


            if (propArray.Length <= 0)
                Console.WriteLine("None PropertyInfo");
            else
            foreach (PropertyInfo prop in propArray)
            {
                objs = prop.GetCustomAttributes(false);
                for (int i = 0; i < objs.Length; i++)
                {
                    sat = (StudentAttribute)objs[i];
                    Console.WriteLine(sat.Description);
                   
                }
            }

            if (methodArray.Length <= 0)
                Console.WriteLine("None MethodInfo");
            else
                foreach (MethodInfo method in methodArray)
                {
                    objs = method.GetCustomAttributes(false);
                    for (int i = 0; i < objs.Length; i++)
                    {
                        if (objs[i] is StudentAttribute)
                        {
                            sat = (StudentAttribute)objs[i];
                            Console.WriteLine(method.Name);
                            Console.WriteLine(sat.Description);
                        }
                    }
                }
            if (typeof(Student).IsDefined(typeof(StudentAttribute),true))
            {
                Console.WriteLine(" StudentAttribute "); 
            }
            Console.ReadLine();  
        }
    }
}