屬性代碼繼承屬性
[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