In X++, you can decorate classes and methods with attributes. Attributes were added in AX 2012 (I believe), where the typical use case was a definition of data contracts. They’re much more common in F&O, because they’re also used for CoC and event handlers.
For most developers, attributes are something defined by Microsoft and used by standard frameworks, but it’s something we can utilize for our own (advanced) scenarios when suitable.
You can easily define your own attributes (an attribute is simply a class inheriting from SysAttribute) and also check whether a class or method has a particular attribute and get attribute parameter values (e.g. the country from CountryRegionAttribute).
To get the attributes, you can use either Dict* classes or the new metadata API.
DictClass and DictMethod classes offer getAttribute() and getAttributes() to get attributes of a specific type, and getAllAttributes() to get all. For example:
DictClass dictClass = new DictClass(classNum(AssetAcquisitionGERContract)); Array attributes = dictClass.getAllAttributes(); for (int i = 1; i <= attributes.lastIndex(); i++) { SysAttribute attribute = attributes.value(i); info(classId2Name(classIdGet(attribute))); }
And here is the same thing with the metadata API:
using Microsoft.Dynamics.AX.Metadata.MetaModel; ... AxClass axClass = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetClass(classStr(AssetAcquisitionGERContract)); var enumerator = (axClass.Attributes as System.Collections.IList).GetEnumerator(); while (enumerator.MoveNext()) { AxAttribute attribute = enumerator.Current; info(attribute.Name); }