自定义特性类, 使得新的类能拥有特性类的属性,方法...
using System;
namespace zengdixiansheng
{
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
public class AttributeClass : Attribute
{
public string name;
public string data;
public AttributeClass(string name)
{
this.name = name;
}
public void Print(string a)
{
Console.WriteLine("print");
}
}
[AttributeClass("zhang", data = "2020.01.15")]
[AttributeClass("yi", data = "2020.01.16")]
public class Text { }
public class DebugClass
{
public static void Debug()
{
Type t = typeof(Text);
object[] attr = t.GetCustomAttributes(typeof(AttributeClass), true);
Console.WriteLine(attr.Length);
foreach (AttributeClass item in attr)
{
item.Print("a");
Console.WriteLine("Name:{0}", item.name);
Console.WriteLine("Name:{0}", item.data);
}
}
}
}