using System;
using System.Reflection;
namespace CustomAttrCS
{
public enum Animal{Dog = 1,Cat,Bird,Pig}
[AttributeUsage(AttributeTargets.Class,Inherited=false,AllowMultiple=true)]
public class AnimalTypeAttribute : Attribute
{
protected Animal thePet;
public Animal Pet
{
get { return thePet; }
set { thePet = Pet; }
}
public AnimalTypeAttribute(Animal pet)
{
thePet = pet;
}
}
[AnimalType(Animal.Pig)]
[AnimalType(Animal.Dog)]
class AnimalTypeTestClass
{
}
class DemoClass
{
static void Main(string[] args)
{
AnimalTypeTestClass testClass = new AnimalTypeTestClass();
Type type = testClass.GetType();
//获取type所拥有的标签.type.GetCustomAttributes(typeof(AnimalTypeAttribute)
AnimalTypeAttribute[] attr = (AnimalTypeAttribute[])type.GetCustomAttributes(typeof(AnimalTypeAttribute), false);
for (int i = 0; i < attr.Length; i++)
{
Console.WriteLine(attr[i].Pet);
}
Console.Read();
}
}
}
using System.Reflection;
namespace CustomAttrCS
{
public enum Animal{Dog = 1,Cat,Bird,Pig}
[AttributeUsage(AttributeTargets.Class,Inherited=false,AllowMultiple=true)]
public class AnimalTypeAttribute : Attribute
{
protected Animal thePet;
public Animal Pet
{
get { return thePet; }
set { thePet = Pet; }
}
public AnimalTypeAttribute(Animal pet)
{
thePet = pet;
}
}
[AnimalType(Animal.Pig)]
[AnimalType(Animal.Dog)]
class AnimalTypeTestClass
{
}
class DemoClass
{
static void Main(string[] args)
{
AnimalTypeTestClass testClass = new AnimalTypeTestClass();
Type type = testClass.GetType();
//获取type所拥有的标签.type.GetCustomAttributes(typeof(AnimalTypeAttribute)
AnimalTypeAttribute[] attr = (AnimalTypeAttribute[])type.GetCustomAttributes(typeof(AnimalTypeAttribute), false);
for (int i = 0; i < attr.Length; i++)
{
Console.WriteLine(attr[i].Pet);
}
Console.Read();
}
}
}