2
Assuming I have the following hierarchy:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class GrandpaAttribute : Attribute
{
public GrandpaAttribute() {}
}
public class ParentAttribute() : GrandpaAttribute
{
public string Name { get; }
public ParentAttribute(string name)
{
Name = name;
}
}
public class ChildAttribute() : ParentAttribute
{
public int Number { get; }
public ChildAttribute(string name, int number) : base(name)
{
Number = number;
}
}
In a certain application, I use the attribute ChildAttribute
:
public class Foo
{
[Child("The Beast", 666)]
public string Description { get; set; }
public DateTime SomeDate { get; set; }
}
The doubt arose:
It is possible to abstract heritage to understand that property Description
has the attribute GrandpaAttribute
?
My intention is to obtain an instance of GrandaAttribute
defined in a class property Foo
as ParentAttribute
, ChildAttribute
or any other attribute derived from it.
Thanks for checking, but I don’t think I expressed myself well. I don’t want to have to use
GetCustomAttribute<ChildAttribute>
, because in the method I’m planning I don’t care specifically which descendant ofGrandaAttribute
is defined, if it isChild
orParent
or any other that I create later... I’ll edit my question including this, it’s relevant information that I missed.– Diego Rafael Souza
Ababei to realize that it works! Using your example in Dotnetfiddle I made the type modification
Child
forGrandpa
and it worked perfectly. I will make this issue and guarantee you the credits. Thank you.– Diego Rafael Souza
@Diegorafaelsouza stay a little without understanding, but, anything leave an explanation what is your ultimate goal
– novic
He just wants
.GetCustomAttribute<GrandpaAttribute>(true);
also return theChildAttribute
. @Diegorafaelsouza It works because an expression:(new ChildAttribute("",1) is GrandpaAttribute)
is true =]– Rovann Linhalis
yeah, I was wondering if the question code was no longer working... rs but I could not give attention
– Rovann Linhalis