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:
Each open instance constructor of a class determines an available sequence of position parameters for a given attribute class.
Each open instance property available for reading and writing determines a named parameter for a given attribute class.
Available types of attribute parameters
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.
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"> </font><font color="#008080">Enum</font><font color="#000000"> UserStatus<br /> None, User, Admin, ISA<br /> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">Enum</font><font color="#000000"> UserStatus;<br /> <br /> </font><font color="#008000">//Attribute class<br /> </font><font color="#008080">Public</font><font color="#000000"> </font><font color="#008080">Class</font><font color="#000000"> UserAttribute: System.Attribute<br /> User: UserStatus;<br /> s: String;<br /> <br /> </font><font color="#008080">Public</font><font color="#000000"> </font><font color="#008080">Constructor</font><font color="#000000"> UserAttribute(Status: UserStatus);<br /> </font><font color="#008080">Begin</font><font color="#000000"><br /> User := Status;<br /> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">Constructor</font><font color="#000000">;<br /> <br /> </font><font color="#008080">Public</font><font color="#000000"> </font><font color="#008080">Property</font><font color="#000000"> Status: UserStatus<br /> </font><font color="#008080">Get</font><font color="#000000"><br /> </font><font color="#008080">Begin</font><font color="#000000"><br /> </font><font color="#008080">Return</font><font color="#000000"> User<br /> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">Get</font><font color="#000000"><br /> </font><font color="#008080">Set</font><font color="#000000"><br /> </font><font color="#008080">Begin</font><font color="#000000"><br /> User := Status;<br /> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">Set</font><font color="#000000"><br /> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">Property</font><font color="#000000">;<br /> <br /> </font><font color="#008080">Public</font><font color="#000000"> </font><font color="#008080">Property</font><font color="#000000"> AdditionalValue: String<br /> </font><font color="#008080">Get</font><font color="#000000"><br /> </font><font color="#008080">Begin</font><font color="#000000"><br /> </font><font color="#008080">Return</font><font color="#000000"> s<br /> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">Get</font><font color="#000000"><br /> </font><font color="#008080">Set</font><font color="#000000"><br /> </font><font color="#008080">Begin</font><font color="#000000"><br /> s := Value;<br /> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">Set</font><font color="#000000"><br /> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">Property</font><font color="#000000">;<br /> </font><font color="#008080">End</font><font color="#000000"> </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: