Attributes

The Fore.NET language enables the developer to determine declarative information about the entities determined in the program. In addition to the standard method of determining information as modifiers, the language provides a new method of determining declarative information named attributes. Developers can assign attributes to various program entities and get attribute information during program runtime. Attribute are determined by describing attribute classes that may have position and named parameters:

Available types of attribute parameters

Attribute Classes

A class that derives from the System.Attribute system class, whether directly or indirectly, is an attribute class. The declaration of an attribute class determines a new kind of attribute that can be further used to append information to entities. Names of all attribute classes end with the Attribute suffix.

NOTE. Uses of an attribute may either include or omit the Attribute suffix.

Setting Attributes

To specify an entity attribute, use the following structure: [<Attribute>(<Parameter 1 values, Parameter 2 values...>)]. All parameters are separated with commas.

Position parameters (if there are any) should stand before the named ones in the order as they are determined in the constructor. Position parameters are mandatory.

Named parameters are optional, they may have any order. Values of named parameters are specified as <Parameter name> := <Value>.

<font color="#008080">Public</font><font color="#000000">&nbsp;</font><font color="#008080">Enum</font><font color="#000000">&nbsp;UserStatus<br /> &nbsp;&nbsp;&nbsp;&nbsp;None,&nbsp;User,&nbsp;Admin,&nbsp;ISA<br /> </font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Enum</font><font color="#000000">&nbsp;UserStatus;<br /> <br /> </font><font color="#008000">//Attribute class<br /> </font><font color="#008080">Public</font><font color="#000000">&nbsp;</font><font color="#008080">Class</font><font color="#000000">&nbsp;UserAttribute:&nbsp;System.Attribute<br /> &nbsp;&nbsp;&nbsp;&nbsp;User:&nbsp;UserStatus;<br /> &nbsp;&nbsp;&nbsp;&nbsp;s:&nbsp;String;<br /> <br /> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Public</font><font color="#000000">&nbsp;</font><font color="#008080">Constructor</font><font color="#000000">&nbsp;UserAttribute(Status:&nbsp;UserStatus);<br /> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Begin</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;:=&nbsp;Status;<br /> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Constructor</font><font color="#000000">;<br /> <br /> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Public</font><font color="#000000">&nbsp;</font><font color="#008080">Property</font><font color="#000000">&nbsp;Status:&nbsp;UserStatus<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Get</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Begin</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Return</font><font color="#000000">&nbsp;User<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Get</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Set</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Begin</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;:=&nbsp;Status;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Set</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Property</font><font color="#000000">;<br /> <br /> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Public</font><font color="#000000">&nbsp;</font><font color="#008080">Property</font><font color="#000000">&nbsp;AdditionalValue:&nbsp;String<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Get</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Begin</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Return</font><font color="#000000">&nbsp;s<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Get</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Set</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Begin</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;:=&nbsp;Value;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Set</font><font color="#000000"><br /> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Property</font><font color="#000000">;<br /> </font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Class</font><font color="#000000">;</font>

Public Class TestAttribute
    
//Attribute with position parameter
    [User(UserStatus.User)]
    
Public Sub UserMethod();
    
Begin
    
End Sub;

    
//Attribute with position and named parameter
    [User(UserStatus.Admin, AdditionalValue := "1")]
    
Public Sub AdminMethod();
    
Begin
    
End Sub;

    [User(UserStatus.ISA, AdditionalValue := 
"2")]
    
Public Sub ISAMethod();
    
Begin
    
End Sub;
End Class;

Imports System;
Imports System.Reflection;
Imports System.Diagnostics;

Sub ExtractAttributeInfo();
Var
    Obj: TestAttribute = 
New TestAttribute();
    ObjType, UserAttrType: System.Type;
    ObjMethodInfo: MethodInfo;
    Attr: System.Attribute;
Begin
    
//Get object type
    ObjType := Obj.GetType();
    
//Get attribute class type
    UserAttrType := (New UserAttribute(UserStatus.None)).GetType();
    
//Get list of methods in class
    For Each ObjMethodInfo In ObjType.GetMethods() Do
        
//Get information about attributes of each class method
        For Each Attr In Attribute.GetCustomAttributes(ObjMethodInfo) Do
            
If Attr.GetType() = UserAttrType Then
                Debug.Write((Attr 
As UserAttribute).Status);
                Debug.WriteLine(
" " + (Attr As UserAttribute).AdditionalValue);
            
End If;
        
End For;
    
End For;
End Sub;

See also:

Fore.NET Language Guide