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);
}
}
}
}
}