User Tools

Site Tools


custom_attributes.htm
Navigation:  Clarion.Net (Clarion#) > Clarion# Language Extensions > CLASS attributes >====== Custom Attributes ====== Previous pageReturn to chapter overviewNext page

NewCNet.jpg

The .NET Framework has more than 100 attributes that are used to control XML serialization, Web services, COM interoperability, and more.

Clarion .NET enables developers to invent new kinds of declarative information, called attributes. It is a totally new language construct that allows the developer to specify information about a class, method, parameter etc. You can then attach these attributes to the various program entities, and retrieve attribute information in a run-time environment.

Attributes are useful if you create your own custom control and want to expose properties in the property grid. There are other uses, but generally they are more esoteric.

The Attribute class is part of .NET framework and hence is available in all languages supported by .NET (including Clarion, C#, VB.NET and VB.NET). Attribute is an abstract base class and any attribute class (custom or .NET provided) is directly or indirectly a subclass.

Custom attributes allow the developer to specify custom details about a programming construct that is both directly visible and available when reading the code (as a comment is), but is also made available throughout the .NET framework.

Custom attributes can be used to store information that can be stored at compile time and retrieved at run time. This information can be related to any aspect/feature of target element depending upon the design of application. To create your own custom attribute class, simply create a new class that inherits System.Attribute. Add whatever properties you want (normally you will also add these to your attribute class constructor), and your custom attribute class is ready for use.

The next step is to determine what elements your attribute can be used on. By default, custom attributes can be applied to all program elements. But you can restrict which types of program elements your custom attribute can be applied to by using the AttributeUsage attribute. The constructor for AttributeUsage takes an enumeration argument of type AttributeTargets (this enumeration is a bit mask, so you can combine values with a bitwise or operation).

You can use the other arguments to AttributeUsage to control whether multiple instances of the same attribute are allowed, and whether derived classes inherit your custom attribute.

Examples:

 PROGRAM
 NAMESPACE(CustomAttributes)
 USING(System.Runtime.InteropServices)
 MAP
[DllImportAttribute('user32.dll', EntryPoint = 'MessageBoxA')]
MessageBox PROCEDURE(UNSIGNED hWnd, STRING Text, STRING Caption, UNSIGNED Type),SIGNED
 END
result SIGNED
 CODE
  result = MessageBox(0, 'Text', 'Caption', 0)
  MESSAGE(result)
!here is another example:
 PROGRAM
 NAMESPACE(Custom)
 USING(System.Runtime.InteropServices)
 MAP
[DllImportAttribute('user32.dll', EntryPoint = 'SetWindowLongA', SetLastError = TRUE)]
SetWindowLong   PROCEDURE(System.IntPtr hwnd, SIGNED nIndex, UNSIGNED dwNewLong), SIGNED
 END

In the first example, the attributes of the MessageBox are extracted and transferred to the Clarion MESSAGE statement for display.

See Also: Platform Invoke Service Support

custom_attributes.htm.txt · Last modified: 2021/04/15 15:57 by 127.0.0.1