Attributes

Attributes are used to provide additional information about elements within your program.

attribute-clause

constructor-parameters property-name = property-value

Example

*> Define attributes on a class
class-id MyClass
          attribute WebService(name Description = "My service")
          attribute Obsolete.

*> Define attributes on a field
01 a binary-long attribute XmlAttribute("UpperB")
          attribute Obsolete static.
01 b string attribute CLSCompliant(false)
*> Define attributes on a property
*> Note that in this case, the attributes must follow
*> the reserved word PROPERTY
01 b binary-long property
          attribute XmlAttribute("UpperB")
          attribute Obsolete static.

*> Define attributes on an event
*> Note that the attributes must follow
*> the reserved word EVENT.
01 d type fred event
          attribute Obsolete static.

*> Define attributes on a method
method-id main 
          attribute Obsolete static.
procedure division
    move 999 to a
    display a

end method.

*> Define attributes on a method parameter and return type
method-id stat static.
procedure division 
          using by value l1 as binary-long
                   attribute CLSCompliant(true)
                   attribute MarshalAs(3)
          returning l2 as binary-long
                   attribute CLSCompliant(true)
                   attribute MarshalAs(2).
    move 999 to a
    display a
    set d to null
end method.

*> Define attributes on a property
method-id set property prop 
          attribute Obsolete static.
procedure division using by value l1 as binary-long.
    move l1 to a
end method.

end class.

*> Define attribute on a delegate
delegate-id fred attribute Obsolete.
procedure division using by value l1 as binary-long.
end delegate.   

To Specify Attributes

You can specify attributes using:

constructor parameters
These specify the parameters of a constructor for that attribute. The constructor parameters used in the attribute specification must match a constructor defined by the attribute. In the following example, the constructor expects a single parameter of type string:
 01 a binary-long attribute xml("UpperB").
NAME property-name = property-value
These are named parameters and correspond to properties defined on the attribute class. The named parameters must match properties defined by the attribute. In the following example, Description is a property of type string defined for the attribute class WebService:
attribute WebService(NAME Description="My service")

About Attributes

Attributes (also known as custom attributes) are used to provide additional information about elements within your program. Attributes may be used to describe methods, data items, properties, events, delegates, method parameters, assemblies, classes and method return values.

Much of the syntax used in a COBOL program describes program elements, such as the visibility of methods and fields (such as PUBLIC, PRIVATE and PROTECTED). All these descriptions are embedded as ‘Metadata’ into the dll or exe file output by the Compiler, and can be interrogated by other programs using Reflection. Attributes are a way of adding additional metadata to program elements in a way that is open ended and unlimited.

The attribute specified becomes part of the metadata describing the subject of the entry and is visible to a reflector.