属性代码
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
class IgnoreAttribute : Attribute
{
}
基类
abstract class ManagementUnit
{
[Ignore]
public abstract byte UnitType { get; }
}
主班
class Region : ManagementUnit
{
public override byte UnitType
{
get { return 0; }
}
private static void Main()
{
Type t = typeof(Region);
foreach (PropertyInfo p in t.GetProperties())
{
if (p.GetCustomAttributes(typeof(IgnoreAttribute), true).Length != 0)
Console.WriteLine("have attr");
else
Console.WriteLine("don't have attr");
}
}
}
输出: don't have attr
解释为什么会这样?毕竟,它必须继承。