I tried to create a custom .NET attribute with the code below but accidentally left off the subclass. This generated an easily-fixed compiler error shown in the comment.
// results in compiler error CS0641: Attribute 'AttributeUsage' is
// only valid on classes derived from System.Attribute
[AttributeUsage(AttributeTargets.Class)]
internal class ToolDeclarationAttribute
{
internal ToolDeclarationAttribute()
{
}
}
My question is how does the compiler know the [AttributeUsage]
attribute can only be applied to a subclass of System.Attribute
? Using .NET Reflector I don't see anything special on the AttributeUsageAttribute
class declaration itself. Unfortunately this might just be a special case generated by the compiler itself.
[Serializable, ComVisible(true), AttributeUsage(AttributeTargets.Class, Inherited=true)]
public sealed class AttributeUsageAttribute : Attribute
{
...
I would like to be able to specify that my custom attribute can only be placed on subclasses of a particular class (or interface). Is this possible?