Attribut CodeGeerbte Attribute
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
class IgnoreAttribute : Attribute
{
}
Basisklasse
abstract class ManagementUnit
{
[Ignore]
public abstract byte UnitType { get; }
}
Hauptklasse
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");
}
}
}
Ausgabe:don't have attr
Erkläre, warum das passiert? Immerhin muss es geerbt werden.
Nein, der Standard für „geerbt“, ist wahr. https://msdn.microsoft.com/en-us/library/system.attributeusageattribute.inherited(v=vs.110).aspx – 00jt