Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify required base class for .NET attribute targets

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?

like image 882
Brian Ensink Avatar asked Jul 27 '09 21:07

Brian Ensink


People also ask

What is the base class for custom attributes defined in net?

The class name AuthorAttribute is the attribute's name, Author , plus the Attribute suffix. It is derived from System. Attribute , so it is a custom attribute class. The constructor's parameters are the custom attribute's positional parameters. In this example, name is a positional parameter.

What is an attribute class in C#?

In C#, attributes are classes that inherit from the Attribute base class. Any class that inherits from Attribute can be used as a sort of "tag" on other pieces of code. For instance, there is an attribute called ObsoleteAttribute . This is used to signal that code is obsolete and shouldn't be used anymore.

How would you define a custom attribute that can only be applied to class code elements?

The following code fragment specifies that a custom attribute can be applied to any class or method: C# Copy. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] End Class.

What are .NET attributes?

Attributes are used for adding metadata, such as compiler instruction and other information such as comments, description, methods and classes to a program. The . Net Framework provides two types of attributes: the pre-defined attributes and custom built attributes.


2 Answers

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?

Actually, there is a way to do this for subclasses (but not interfaces) using protected - see Restricting Attribute Usage. To reproduce the code (but not the discussion):

abstract class MyBase {
    [AttributeUsage(AttributeTargets.Property)]
    protected sealed class SpecialAttribute : Attribute {}
}
class ShouldBeValid : MyBase {
    [Special] // works fine
    public int Foo { get; set; }
}
class ShouldBeInvalid { // not a subclass of MyBase
    [Special] // type or namespace not found
    [MyBase.Special] // inaccessible due to protection level
    public int Bar{ get; set; }
}
like image 105
Marc Gravell Avatar answered Oct 13 '22 07:10

Marc Gravell


AttributeUsageAttribute is just a magic class (like Attribute itself is). This is a built-in compiler rule, and you cannot do something like that for your own attributes.

like image 28
Pavel Minaev Avatar answered Oct 13 '22 06:10

Pavel Minaev