Generating C# attributes

 

Types, members, and other entities in a C# program support modifiers that control certain aspects of their behavior. For example, the accessibility of a method is controlled using the "public", "protected", "internal" and "private" modifiers. C# generalizes this capability such that user-defined types of declarative information can be attached to program entities and retrieved at runtime. Programs specify this additional declarative information by defining and using attributes.


To declare an attribute you must add the "CsAttribute" stereotype to your class and define which elements your C# attribute targets.


By double clicking on a class stereotyped "CsAttribute", the dialog box shown in Figure 39 below is displayed.

 

Figure 40. The "C# attribute definition" window


Legend:

·         The "Name" field shows the name of the C# attribute.

·         The "Allow multiple" tickbox indicates whether or not the multiplicity of the C# attribute is allowed. By default, this tickbox is not checked. If the {CsAttributeAllowMultiple} tagged value is present on the C# attribute, then the tickbox is automatically checked. If you check this tickbox, the {CsAttributeAllowMultiple} tagged value is created on the C# attribute. If you uncheck this tickbox, this tagged value is destroyed.

·         The "Attribute is inherited" tickbox indicates whether or not the C# attribute is inherited. By default, this tickbox is not checked. If the {CsAttributeInheritance} tagged value is present on the C# attribute, then the tickbox is automatically checked. If you check this tickbox, the {CsAttributeInheritance} tagged value is created on the C# attribute. If you uncheck this tickbox, this tagged value is destroyed.

·         The "All" tickbox is used to activate all possible target types and define the tree as being inactive (if the tickbox is checked).

·         The elements which figure in the list of attribute targets are initialized with the parameters of the {CsAttributeTarget} tagged value, if this is present.

 

Note:      If no tickboxes are checked in the "Attribute targets" zone, the "All" tickbox is automatically checked.



After validation the following tagged values are added to your class:

·         {CsAttributeTarget(…)} with the selected target types (here {CsAttributeTarget(All)}).

·         {CsAttributeAllowMultiple} if the “Allow multiple” tickbox has been checked. If this is not the case, this tagged value is not added.

·         {CsAttributeInheritance} if the “Attribute is inherited” tickbox has been checked. If this is not the case, this tagged value is not added.



Your attribute can target :

·         Assembly

·         Module

·         Class

·         Struct

·         Enum

·         Constructor

·         Method

·         Property

·         Field

·         Event

·         Interface

·         Parameter

·         Delegate

·         ReturnValue

·         All

 

Note:      This list is a C# static enumeration, and no new target types can be added.

 

 

UML model

C# generated

using System;

using System.Diagnostics;

 

 

namespace CsAttributeSample

{

 

    [AttributeUsage(AttributeTargets.All, Inherited=false, AllowMultiple=true)]

    public class Author : System.Attribute

    {

        public Author(string EmailValue, string AuthorValue)

        {

 

 

        }

 

#region email

        public string Email;

#endregion

 

#region author

        public string Author;

#endregion

 

    }

 

}