C# Attribute特性 (一)

 

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Reflection;

namespace Attributes

{

  [AttributeUsage(AttributeTargets.All,AllowMultiple=true,Inherited=true)]

  public class DeveloperAttribute : System.Attribute

  {

      public DeveloperAttribute(string name, string level)

      {

          this.Name = name;

          this.Level = level;

          this.Reviewed = false;

      }

      public string Name

      {

          get;

        private set;

      }

      public string Level

      {

          get;

        private set;

      }

      public bool Reviewed

      {

          get;

          set;

      }

 

  //多个实例

  [Developer("Joan Smith", "1")]

  [Developer("Joan Smith", "1", Reviewed = true)]

  public class MainApps

  {     

  }

  //单个实例

  [Developer("Joan Smith", "1", Reviewed = true)]

  class MainApp

  {

      static void Main(string[] args)

      {

          GetAttribute(typeof(MainApp));

          Console.WriteLine("-----------------------------------------------------");

          GetAttributes(typeof(MainApps));         

          Console.WriteLine("-----------------------------------------------------");         

      }

      //检索属性的一个实例GetCustomAttribute

      public static void GetAttribute(Type t)

      {

          DeveloperAttribute myAttribute = (DeveloperAttribute)Attribute.GetCustomAttribute(t, typeof(DeveloperAttribute));

          if (null == myAttribute)

          {

              Console.WriteLine("The Atrribute is null");

          }

          else

          {

              Console.WriteLine("Name:{0}", myAttribute.Name);

              Console.WriteLine("Level:{0}", myAttribute.Level);

              Console.WriteLine("Reviewed:{0}", myAttribute.Reviewed);

          }

      }

      //检索应用到同一范围的属性的多个实例GetCustomAttributes

      public static void GetAttributes(Type t)

      {

          DeveloperAttribute[] myAttribute = (DeveloperAttribute[])Attribute.GetCustomAttributes(t, typeof(DeveloperAttribute));//检索属性的多个实例

          if (null == myAttribute)

          {

              Console.WriteLine("The Atrribute Array is null");

          }

          else

          {

              for (int i = 0; i < myAttribute.Length; i++)

              {

                  Console.WriteLine("Name:{0}", myAttribute[i].Name);

                  Console.WriteLine("Level:{0}", myAttribute[i].Level);

                  Console.WriteLine("Reviewed:{0}", myAttribute[i].Reviewed);

              }

          }

      }

      //检索应用到不同范围的属性的多个实例

      public static void GetAttributeAreas(Type t)

      {

          DeveloperAttribute att;

          //Get the class-level attributes.

          //Put the instance of the attribute on the class level in the att object.

          att = (DeveloperAttribute)Attribute.GetCustomAttribute(t, typeof(DeveloperAttribute));

          if (null == att)

          {

              Console.WriteLine("No attribute in class {0}.\n", t.ToString());

          }

          else

          {

              Console.WriteLine("The Name Attribute on the class level is: {0}.", att.Name);

              Console.WriteLine("The Level Attribute on the class level is: {0}.", att.Level);

              Console.WriteLine("The Reviewed Attribute on the class level is: {0}.\n", att.Reviewed);

          }

          //Get the method-level attributes.

          //Get all methods in this class, and put them

          //in an array of System.Reflection.MemberInfo objects.

          MemberInfo[] MyMemberInfo = t.GetMethods();

          //Loop through all methods in this class that are in the

          //MyMemberInfo array.

          for (int i = 0; i < MyMemberInfo.Length; i++)

          {

              att = (DeveloperAttribute)Attribute.GetCustomAttribute(MyMemberInfo[i], typeof(DeveloperAttribute));

              if (null == att)

              {

                  Console.WriteLine("No attribute in member function {0}.\n", MyMemberInfo[i].ToString());

              }

              else

              {

                  Console.WriteLine("The Name Attribute for the {0} member is: {1}.", MyMemberInfo[i].ToString(), att.Name);

                  Console.WriteLine("The Level Attribute for the {0} member is: {1}.", MyMemberInfo[i].ToString(), att.Level);

                  Console.WriteLine("The Reviewed Attribute for the {0} member is: {1}.\n", MyMemberInfo[i].ToString(), att.Reviewed);

              }

          }

      }

  }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值