Geerbte Attribute

Attribut code

[AttributeUsage(AttributeTargets.Property, Inherited = true)]
class IgnoreAttribute : Attribute
{
}

Basisklasse

abstract class ManagementUnit
{
    [Ignore]
    public abstract byte UnitType { get; }
}

Main-Klasse

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ären, warum dies passiert ist? Nach allem muss es geerbt.

InformationsquelleAutor turbanoff | 2012-04-27
Schreibe einen Kommentar