属性代码继承属性
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
class IgnoreAttribute : Attribute
{
}
基类
abstract class ManagementUnit
{
[Ignore]
public abstract byte UnitType { get; }
}
Main类
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
解释为什么会发生这种情况?毕竟,它必须继承。
不,“继承”的默认值是true。 https://msdn.microsoft.com/en-us/library/system.attributeusageattribute.inherited(v=vs.110).aspx – 00jt 2015-08-13 18:08:50