Is it possible to obtain a parent attribute when the class defines the child attribute?

Asked

Viewed 72 times

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.

1 answer

2


From what I understand, you want to, get into the class instance GrandpaAttribute, for test reasons I added a property First for the test to be performed, first the real instance is sought, and then with a cast arrives in class GrandpaAttribute, example:

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);
    }
}

An Online Dotnetfiddle Example

  • 1

    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 of GrandaAttribute is defined, if it is Child or Parent or any other that I create later... I’ll edit my question including this, it’s relevant information that I missed.

  • 1

    Ababei to realize that it works! Using your example in Dotnetfiddle I made the type modification Child for Grandpa and it worked perfectly. I will make this issue and guarantee you the credits. Thank you.

  • @Diegorafaelsouza stay a little without understanding, but, anything leave an explanation what is your ultimate goal

  • 1

    He just wants .GetCustomAttribute<GrandpaAttribute>(true); also return the ChildAttribute. @Diegorafaelsouza It works because an expression: (new ChildAttribute("",1) is GrandpaAttribute) is true =]

  • yeah, I was wondering if the question code was no longer working... rs but I could not give attention

Browser other questions tagged

You are not signed in. Login or sign up in order to post.