AttributesSample.

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

namespace ConsoleApplication1
{
    /// <summary>
    /// AttributeTargets.Class表示我们自定义的属性只能用于类上。
    /// </summary>
    [AttributeUsage(AttributeTargets.Class)]
    public class MyCustomAttribute : Attribute
    {
        private string attributevalue;
        public MyCustomAttribute(string AttributeValue)
        {
            attributevalue = AttributeValue;
        }

        public string AttributeValue
        {
            get
            {
                return attributevalue;
            }
        }
    }

    /// <summary>
    /// 为了使用我们自定义的属性,下面这个类是一个基类。其它的类要使用这个AttributeValue的话就得继承这个基类。
    /// </summary>
    public class MyCustom
    {
        public string AttributeValue
        {
            get
            {
                string Value = null;
                Type type = this.GetType();
                MyCustomAttribute[] attribs = (MyCustomAttribute[])type.GetCustomAttributes(typeof(MyCustomAttribute), true);
                if (attribs.Length > 0)
                {
                    MyCustomAttribute attrib = attribs[0];
                    Value = attrib.AttributeValue;
                }
                return Value;
            }
        }
    }

    /// <summary>
    /// 这个类用来测试我们自定义的属性,它继承了MyCustom才能通过Property来访问
    /// </summary>
    [MyCustom("Test")]
    class TestClass : MyCustom
    {

    }

    class Program44ju
    {
        static void Main34(string[] args)
        {
            TestClass test = new TestClass();
            Console.WriteLine("The value of the attribute is: " + test.AttributeValue);         
        }


        Dictionary<string, string> GetAliasListing(Type destinationType)
        {
            //Get all the properties that are in the
            // destination type.
            PropertyInfo[] destinationProperties = destinationType.GetProperties();
            Dictionary<string, string> aliases = new Dictionary<string, string>();

            foreach (PropertyInfo property in destinationProperties)
            {
                //Get the alias attributes.
                object[] aliasAttributes = property.GetCustomAttributes(typeof(Alias), true);

                //Loop through the alias attributes and add them to the dictionary.
                foreach (object attribute in aliasAttributes)
                {
                    foreach (string name in ((Alias)attribute).Names)
                    {
                        aliases.Add(name, property.Name);
                    }
                    //We also need to add the property name as an alias.
                    aliases.Add(property.Name, property.Name);
                }
            }
            return aliases;
        }
    }

    class Alias : System.Attribute
    {
        string[] _names;
        public Alias(params string[] names)
        {
            this.Names = names;
        }
        public string[] Names
        {
            get { return _names; }
            set { _names = value; }
        }
    }

}

posted on 2011-11-15 22:28  breakpoint  阅读(89)  评论(0编辑  收藏  举报

导航