CSharp · Console
using System;
using System.Reflection;
[System.AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class GrandpaAttribute : Attribute
{
public GrandpaAttribute()
{
First = "Teste";
}
public string First {get;set;}
}
public class ParentAttribute : GrandpaAttribute
{
public string Name { get; private set; }
public ParentAttribute(string name)
{
Name = name;
}
}
public class ChildAttribute : ParentAttribute
{
public int Number { get; private set; }
public ChildAttribute(string name, int number)
: base(name)
{
Number = number;
}
}
public class Foo
{
[Child("The Beast", 666)]
public string Description { get; set; }
public DateTime SomeDate { get; set; }
}
public class Program
{
public static void Main()
{
ChildAttribute childAttribute = typeof(Foo)
.GetProperty("Description")
.GetCustomAttribute<ChildAttribute>(true);
Console.WriteLine(((GrandpaAttribute)childAttribute).First);
}
}