đang Thuộc tínhthuộc tính thừa kế
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
class IgnoreAttribute : Attribute
{
}
class Base
abstract class ManagementUnit
{
[Ignore]
public abstract byte UnitType { get; }
}
lớp 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");
}
}
}
Đầu ra:don't have attr
Giải thích lý do tại sao điều này xảy ra? Sau khi tất cả, nó phải thừa kế.
Không, mặc định cho "được kế thừa" là đúng. https://msdn.microsoft.com/en-us/library/system.attributeusageattribute.inherited(v=vs.110).aspx – 00jt