2

I have attribute

[AttributeUsage(AttributeTargets.Class)]
public class CacheAttribute : Attribute
{        
}

and generic class

[Cache]    
public class BaseClass<T>
{        
}

and another class

public class ImplClass : BaseClass<Type>
{        
}

When i try to get custom attributes from

BaseClass<Type>

I get my attribute, but not from

ImplClass 

My code to get Custom attributes is

typeof(ImplClass).CustomAttributes -> 0
typeof(BaseClass<Type>).CustomAttributes -> 1

How can i inherite attribute to my implementation?

NOTE: There is a similar question How does inheritance work for Attributes? that is not helpful. Unlike that issue, this one is about attribute inheritance in combination with generics.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Horosho
  • 647
  • 1
  • 8
  • 22
  • @Sinatr The mentioned already answered quetion is not the same. – Horosho Feb 21 '18 at 14:26
  • DavidG The mentioned already answered question is not the same. Here is generic issue – Horosho Feb 21 '18 at 14:26
  • 1
    I suggest adding the Reflection code to your question that you are using to read the attribute. If this is indeed a new question related specifically to generics, it can be reopened. – NightOwl888 Feb 21 '18 at 15:18
  • @NightOwl888 it's trivial like typeof(ImplClass).CustomAttributes – Horosho Feb 22 '18 at 06:18
  • Even trivial code can potentially have bugs in it. Besides, you need to provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) as proof this is not a duplicate of the other question, so it can be reopened. How are we supposed to be able to run this to confirm what you are saying is true and provide a solution if we don't have the same code as you? There is currently nothing runnable in your question. – NightOwl888 Feb 22 '18 at 06:27
  • @NightOwl888 ok, understood – Horosho Feb 22 '18 at 06:30
  • Possible duplicate of [How does inheritance work for Attributes?](https://stackoverflow.com/questions/1240960/how-does-inheritance-work-for-attributes) – Andriy Tolstoy Feb 22 '18 at 08:54

1 Answers1

3

Your attribute is inherited by your child class. The problem you encounter is not specific to generics. It can easily be reproduced with non-generic types:

using System.Linq;

[AttributeUsage(AttributeTargets.Class)]
public class FooAttribute : Attribute
{
    private readonly string name;

    public FooAttribute(string name)
    {
        this.name = name;
    }
}

[Foo("bar")]
public class Base { }
public class Child : Base { }

Console.WriteLine(typeof(Base).CustomAttributes.Count());  // Prints 1.
Console.WriteLine(typeof(Child).CustomAttributes.Count()); // Prints 0.

From the documentation of MemberInfo.CustomAttributes:

Gets a collection that contains this member's custom attributes.

So this property just gets the attributes applied to the type itself, not inherited ones (the implementation in MemberInfo calls GetCustomAttributesData which in turn just throws an NotImplementedException and i could not find an override in Type so i assume this to be right).

However you can use one of the MemberInfo.GetCustomAttributes overloads or one of these extension methods to get inherited attributes

typeof(ImplClass).GetCustomAttributes(typeof(CacheAttribute), inherit: true)
Streamline
  • 952
  • 1
  • 15
  • 23