C#idiomatic C#
writing-custom-attributesstandard/attributes

Core library

Write custom attributes

Design your own custom attributes in .NET. Custom attributes are essentially classes derived directly or indirectly from System.Attribute.

  • page standard/attributes/writing-custom-attributes
  • updated 03/02/2026

To design custom attributes, you don't need to learn many new concepts. If you're familiar with object-oriented programming and know how to design classes, you already have most of the knowledge needed. Custom attributes are traditional classes that derive directly or indirectly from the System.Attribute class. Just like traditional classes, custom attributes contain methods that store and retrieve data.

The primary steps to properly design custom attribute classes are as follows:

This section describes each of these steps and concludes with a custom attribute example.

Applying the AttributeUsageAttribute

A custom attribute declaration begins with the System.AttributeUsageAttribute attribute, which defines some of the key characteristics of your attribute class. For example, you can specify whether your attribute can be inherited by other classes or which elements the attribute can be applied to. The following code fragment demonstrates how to use the System.AttributeUsageAttribute:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs5

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb5

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

The System.AttributeUsageAttribute has three members that are important for the creation of custom attributes: AttributeTargets, Inherited, and AllowMultiple.

AttributeTargets Member

In the preceding example, System.AttributeTargets.All is specified, indicating that this attribute can be applied to all program elements. Alternatively, you can specify System.AttributeTargets.Class, indicating that your attribute can be applied only to a class, or System.AttributeTargets.Method, indicating that your attribute can be applied only to a method. All program elements can be marked for description by a custom attribute in this manner.

You can also pass multiple System.AttributeTargets values. The following code fragment specifies that a custom attribute can be applied to any class or method:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs6

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb6

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

Inherited Property

The System.AttributeUsageAttribute.Inherited property indicates whether your attribute can be inherited by classes that are derived from the classes to which your attribute is applied. This property takes either a true (the default) or false flag. In the following example, MyAttribute has a default System.AttributeUsageAttribute.Inherited value of true, while YourAttribute has an System.AttributeUsageAttribute.Inherited value of false:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs7

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb7

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

The two attributes are then applied to a method in the base class MyClass:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs9

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb9

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

Finally, the class YourClass is inherited from the base class MyClass. The method MyMethod shows MyAttribute but not YourAttribute:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs10

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb10

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

AllowMultiple Property

The System.AttributeUsageAttribute.AllowMultiple property indicates whether multiple instances of your attribute can exist on an element. If set to true, multiple instances are allowed. If set to false (the default), only one instance is allowed.

In the following example, MyAttribute has a default System.AttributeUsageAttribute.AllowMultiple value of false, while YourAttribute has a value of true:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs11

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb11

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

When multiple instances of these attributes are applied, MyAttribute produces a compiler error. The following code example shows the valid use of YourAttribute and the invalid use of MyAttribute:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs13

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb13

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

If both the System.AttributeUsageAttribute.AllowMultiple property and the System.AttributeUsageAttribute.Inherited property are set to true, a class that's inherited from another class can inherit an attribute and have another instance of the same attribute applied in the same child class. If System.AttributeUsageAttribute.AllowMultiple is set to false, the values of any attributes in the parent class will be overwritten by new instances of the same attribute in the child class.

Declaring the Attribute Class

After you apply the System.AttributeUsageAttribute, start defining the specifics of your attribute. The declaration of an attribute class looks similar to the declaration of a traditional class, as demonstrated by the following code:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs14

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb14

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

This attribute definition demonstrates the following points:

  • Attribute classes must be declared as public classes.

  • By convention, the name of the attribute class ends with the word Attribute. While not required, this convention is recommended for readability. When the attribute is applied, the inclusion of the word Attribute is optional.

  • All attribute classes must inherit directly or indirectly from the System.Attribute class.

  • In Microsoft Visual Basic, all custom attribute classes must have the System.AttributeUsageAttribute attribute.

Declaring Constructors

Just like traditional classes, attributes are initialized with constructors. The following code fragment illustrates a typical attribute constructor. This public constructor takes a parameter and sets a member variable equal to its value.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs15

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb15

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

You can overload the constructor to accommodate different combinations of values. If you also define a property for your custom attribute class, you can use a combination of named and positional parameters when initializing the attribute. Typically, you define all required parameters as positional and all optional parameters as named. In this case, the attribute can't be initialized without the required parameter. All other parameters are optional.

Constructor parameters and public properties of an attribute are restricted to a limited set of types because the runtime must be able to read the attribute values directly from metadata. The valid attribute parameter types are:

If you define a constructor that accepts a type outside this list, the attribute compiles successfully, but a compiler error occurs when you try to apply it. For more information about what expressions are allowed when applying an attribute, see Apply attributes.

The following code example shows how an attribute that uses the previous constructor can be applied using optional and required parameters. It assumes that the attribute has one required Boolean value and one optional string property.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs17

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb17

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

Declaring Properties

If you want to define a named parameter or provide an easy way to return the values stored by your attribute, declare a property. Attribute properties should be declared as public entities with a description of the data type that will be returned. Define the variable that will hold the value of your property and associate it with the get and set methods. The following code example demonstrates how to implement a property in your attribute:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs16

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb16

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

Custom Attribute Example

This section incorporates the previous information and shows how to design an attribute that documents information about the author of a section of code. The attribute in this example stores the name and level of the programmer, and whether the code has been reviewed. It uses three private variables to store the actual values to save. Each variable is represented by a public property that gets and sets the values. Finally, the constructor is defined with two required parameters:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs4

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb4

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

You can apply this attribute using the full name, DeveloperAttribute, or using the abbreviated name, Developer, in one of the following ways:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source2.cs12

csharp
using System;

namespace CustomCodeAttributes
{
    //<snippet4>
    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : Attribute
    {
        // Private fields.
        private string name;
        private string level;
        private bool reviewed;

        // This constructor defines two required parameters: name and level.

        public DeveloperAttribute(string name, string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;
        }

        // Define Name property.
        // This is a read-only attribute.

        public virtual string Name
        {
            get {return name;}
        }

        // Define Level property.
        // This is a read-only attribute.

        public virtual string Level
        {
            get {return level;}
        }

        // Define Reviewed property.
        // This is a read/write attribute.

        public virtual bool Reviewed
        {
            get {return reviewed;}
            set {reviewed = value;}
        }
    }
    //</snippet4>
}

namespace CustomCodeAttributes_Examples1
{
    //<snippet5>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    //</snippet5>
    public class DummyAttribute1 : Attribute
    {
    }

    //<snippet6>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    //</snippet6>
    public class DummyAttribute2 : Attribute
    {
    }

    //<snippet7>
    // This defaults to Inherited = true.
    public class MyAttribute : Attribute
    {
        //...
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = false)]
    public class YourAttribute : Attribute
    {
        //...
    }
    //</snippet7>

    //<snippet9>
    public class MyClass
    {
        [MyAttribute]
        [YourAttribute]
        public virtual void MyMethod()
        {
            //...
        }
    }
    //</snippet9>

    //<snippet10>
    public class YourClass : MyClass
    {
        // MyMethod will have MyAttribute but not YourAttribute.
        public override void MyMethod()
        {
            //...
        }
    }
    //</snippet10>
}

namespace CustomCodeAttributes_Examples2
{
    //<snippet11>
    //This defaults to AllowMultiple = false.
    public class MyAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class YourAttribute : Attribute
    {
    }
    //</snippet11>

// #if'd out since MyClass will intentionally not compile
#if false
    //<snippet12>
    [Developer("Joan Smith", "1")]

    -or-

    [Developer("Joan Smith", "1", Reviewed = true)]
    //</snippet12>

    //<snippet13>
    public class MyClass
    {
        // This produces an error.
        // Duplicates are not allowed.
        [MyAttribute]
        [MyAttribute]
        public void MyMethod()
        {
            //...
        }

        // This is valid.
        [YourAttribute]
        [YourAttribute]
        public void YourMethod()
        {
            //...
        }
    }
    //</snippet13>
#endif
}

namespace CustomCodeAttributes_Examples3
{
    //<snippet14>
    [AttributeUsage(AttributeTargets.Method)]
    public class MyAttribute : Attribute
    {
        // . . .
    }
    //</snippet14>
}

namespace CustomCodeAttributes_Examples4
{
    public class MyAttribute : Attribute
    {
        private bool myvalue;
        private string myoptional;

        //<snippet15>
        public MyAttribute(bool myvalue)
        {
            this.myvalue = myvalue;
        }
        //</snippet15>

        //<snippet16>
        public bool MyProperty
        {
            get {return this.myvalue;}
            set {this.myvalue = value;}
        }
        //</snippet16>

        public string OptionalParameter
        {
            get {return this.myoptional;}
            set {this.myoptional = value;}
        }
    }

    //<snippet17>
    // One required (positional) and one optional (named) parameter are applied.
    [MyAttribute(false, OptionalParameter = "optional data")]
    public class SomeClass
    {
        //...
    }
    // One required (positional) parameter is applied.
    [MyAttribute(false)]
    public class SomeOtherClass
    {
        //...
    }
    //</snippet17>
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.attributes.usage/vb/source2.vb12

vb
Namespace CustomCodeAttributes
    '<snippet4>
    <AttributeUsage(AttributeTargets.All)>
    Public Class DeveloperAttribute
        Inherits Attribute
        ' Private fields.
        Private myname As String
        Private mylevel As String
        Private myreviewed As Boolean

        ' This constructor defines two required parameters: name and level.

        Public Sub New(name As String, level As String)
            Me.myname = name
            Me.mylevel = level
            Me.myreviewed = False
        End Sub

        ' Define Name property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Name() As String
            Get
                Return myname
            End Get
        End Property

        ' Define Level property.
        ' This is a read-only attribute.

        Public Overridable ReadOnly Property Level() As String
            Get
                Return mylevel
            End Get
        End Property

        ' Define Reviewed property.
        ' This is a read/write attribute.

        Public Overridable Property Reviewed() As Boolean
            Get
                Return myreviewed
            End Get
            Set
                myreviewed = value
            End Set
        End Property
    End Class
    '</snippet4>
End Namespace

Namespace CustomCodeAttributes_Examples1
    '<snippet5>
    <AttributeUsage(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)>
    Public Class SomeClass
        Inherits Attribute
        '...
    End Class
    '</snippet5>

    '<snippet6>
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)>
    Public Class SomeOtherClass
        Inherits Attribute
        '...
    End Class
    '</snippet6>

    '<snippet7>
    ' This defaults to Inherited = true.
    Public Class MyAttribute
        Inherits Attribute
        '...
    End Class

    <AttributeUsage(AttributeTargets.Method, Inherited:=False)>
    Public Class YourAttribute
        Inherits Attribute
        '...
    End Class
    '</snippet7>

    '<snippet9>
    Public Class MeClass
        <MyAttribute>
        <YourAttribute>
        Public Overridable Sub MyMethod()
            '...
        End Sub
    End Class
    '</snippet9>

    '<snippet10>
    Public Class YourClass
        Inherits MeClass
        ' MyMethod will have MyAttribute but not YourAttribute.
        Public Overrides Sub MyMethod()
            '...
        End Sub

    End Class
    '</snippet10>
End Namespace

Namespace CustomCodeAttributes_Examples2
    '<snippet11>
    ' This defaults to AllowMultiple = false.
    Public Class MyAttribute
        Inherits Attribute
    End Class

    <AttributeUsage(AttributeTargets.Method, AllowMultiple:=true)>
    Public Class YourAttribute
        Inherits Attribute
    End Class
    '</snippet11>

    ' #if'd out since MyClass will intentionally not compile
#If False
    '<snippet12>
    <Developer("Joan Smith", "1")>

    -or-

    <Developer("Joan Smith", "1", Reviewed := true)>
    '</snippet12>

    '<snippet13>
    Public Class MyClass
        ' This produces an error.
        ' Duplicates are not allowed.
        <MyAttribute>
        <MyAttribute>
        Public Sub MyMethod()
            '...
        End Sub

        ' This is valid.
        <YourAttribute>
        <YourAttribute>
        Public Sub YourMethod()
            '...
        End Sub
    End Class
    '</snippet13>
#End If
End Namespace

Namespace CustomCodeAttributes_Examples3
    '<snippet14>
    <AttributeUsage(AttributeTargets.Method)>
    Public Class MyAttribute
        Inherits Attribute
        ' . . .
    End Class
    '</snippet14>
End Namespace

Namespace CustomCodeAttributes_Examples4
    Public Class MyAttribute
        Inherits Attribute

        Private myvalue As Boolean
        Private myoptional As String

        '<snippet15>
        Public Sub New(myvalue As Boolean)
            Me.myvalue = myvalue
        End Sub
        '</snippet15>

        '<snippet16>
        Public Property MyProperty As Boolean
            Get
                Return Me.myvalue
            End Get
            Set
                Me.myvalue = Value
            End Set
        End Property
        '</snippet16>

        Public Property OptionalParameter As String
            Get
                Return Me.myoptional
            End Get
            Set
                Me.myoptional = Value
            End Set
        End Property
    End Class

    '<snippet17>
    ' One required (positional) and one optional (named) parameter are applied.
    <MyAttribute(false, OptionalParameter:="optional data")>
    Public Class SomeClass
        '...
    End Class

    ' One required (positional) parameter is applied.
    <MyAttribute(false)>
    Public Class SomeOtherClass
        '...
    End Class
    '</snippet17>
End Namespace

The first example shows the attribute applied with only the required named parameters. The second example shows the attribute applied with both the required and optional parameters.

See also