c#中使用特性和反射的小例子

128 阅读1分钟

我的例程里面编写了2个自定义特性,一个是MyAttribute,一个是MyAttribute2,其实就是两个继承自System.Attribute的类
然后在主函数前面定义一个MyClass类,并为这个类添加上述两个自定义特性。
然后在主函数里面通过反射来获取MyClass类的所有特性,并输出。
运行结果入如下:在这里插入图片描述

MyAttribute.cs代码如下:

using System;

namespace ConsoleApplication1
{
    //自定义一个特性,MyAttribute:
    [AttributeUsage(AttributeTargets.All)]//该特性适用于所有对象
    public class MyAttribute : System.Attribute//我的特性继承自System.Attribute
    {
        public readonly string Url;
        private string _myName;
        public string myName//myName 是一个命名(named)参数
        {
            get
            {
                return _myName;
            }
            set
            {
                _myName = value;
            }
        }
        private int _attributeID;
        public int attributeID
        {
            get
            {
                return _attributeID;
            }
            set
            {
                _attributeID = value;
            }
        }

        public MyAttribute(string name, int nID)
        {
            this.myName = name;
            this.attributeID = nID;
        }
    }
}

MyAttribute2.cs代码如下:

using System;

namespace ConsoleApplication1
{
    //自定义第二个特性,MyAttribute2:
    [AttributeUsage(AttributeTargets.Class)]//该特性适用于所有对象
    public class MyAttribute2 : System.Attribute//我的特性继承自System.Attribute
    {
        public readonly string Url;
        private string _myName;
        public string myName//myName 是一个命名(named)参数
        {
            get
            {
                return _myName;
            }
            set
            {
                _myName = value;
            }
        }
        private int _attributeID;
        public int attributeID
        {
            get
            {
                return _attributeID;
            }
            set
            {
                _attributeID = value;
            }
        }
        public MyAttribute2(string name,int nID)
        {
            this.myName = name;
            this.attributeID = nID;
        }
    }
}

Program.cs代码如下:

using System;

namespace ConsoleApplication1
{
    [MyAttribute("this is my attribute!",1)]//使用第一个特性
    [MyAttribute2("this is my attribute!", 2)]//使用第二个特性
    class MyClass
    {

    }
    class Program
    {
        static void Main(string[] args)
        {
            //使用反射来显示类的特性:
            System.Reflection.MemberInfo info = typeof(MyClass);
            object[] attributes = info.GetCustomAttributes(true);//搜索此成员的继承链以查找这些属性,则为 true;否则为 false
            //遍历MyClass类的特性,并输出特性名称
            for (int i = 0;i<attributes.Length;i++)
            {
                Console.WriteLine("特性:"+attributes[i].ToString());                
            }

            object[] fattributes = info.GetCustomAttributes(false);
            //输出特性里面的参数:
            MyAttribute myat = (MyAttribute)fattributes[0];
            Console.WriteLine("我的特性里面的myName变量值:{0},特性ID:{1}", myat.myName, myat.attributeID);
            MyAttribute2 myat2 = (MyAttribute2)fattributes[1];
            Console.WriteLine("我的特性里面的myName变量值:{0},特性ID:{1}", myat2.myName, myat2.attributeID);
            Console.ReadKey();
        }
    }
}