现在的位置: 首页 > 综合 > 正文

元数据(二)

2013年09月15日 ⁄ 综合 ⁄ 共 921字 ⁄ 字号 评论关闭

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
 {
 
 }

}

这里的东西是这样的意思,有一个元数据,叫做颜色。

有一个东西叫做动物,她的颜色是红色的。

动物中有一种叫做猫,他是蓝色的。

有一种叫做狗,他是黄色的。

抱歉!评论已关闭.