using System;
namespace ConsoleApplication2
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
System.Reflection.MemberInfo info=typeof(Cat);
Color colorA = (Color)Attribute.GetCustomAttribute(info,typeof(Color));
if(colorA!=null)
{
Console.WriteLine("color of cat is:{0}",colorA.color);
}
info=typeof(Animal);
colorA = (Color)Attribute.GetCustomAttribute(info,typeof(Color));
if(colorA!=null)
{
Console.WriteLine("color of animal is:{0}",colorA.color);
}
}
}
[AttributeUsage(AttributeTargets.Class)]
public class Color : Attribute{
public string color;
public Color(string color){
this.color=color;
}
}
[Color("red")]
public class Animal
{
}
[Color("blue")]
public class Cat:Animal
{
}
[Color("yellow")]
public class Dog:Animal
{
}
}
这里的东西是这样的意思,有一个元数据,叫做颜色。
有一个东西叫做动物,她的颜色是红色的。
动物中有一种叫做猫,他是蓝色的。
有一种叫做狗,他是黄色的。