Wednesday, July 2, 2014

Concept of reflection and its implmentation in Dynamics AX

Key benefits of applying the concept of reflection in your programming are:
  • It gives you run-time access to a variety of class information.
  • It even lets you read and write fields and call methods of a class selected at run time.
  • With reflection, you can make your own tools to audit code or to generate code.
I found a useful article to understand reflection in programming that I would like to share with you guys: Reflection – Where and why As an example, I have written the code in X++ which would traverse through all the classes of AOT and list down the methods.

static void TraverseAOTTableMethods(Args _args)
{
SysDictTable sysDictTable;
SysDictMethod sysDictMethod;
DictMethod dictMethod;
int ind;
TreeNode curTable, methods, curMethod;
TreeNodeIterator tableiter, methodIter;
tableiter = TreeNode::findNode(@'\Data dictionary\Tables').AOTiterator();

if (tableiter)
{
curTable = tableiter.next();

while (curTable)
{
ind=1;
methods = curTable.AOTfindChild("Methods");
methodIter = methods.AOTiterator();

if (methodIter)
{
curMethod = methodIter.next();

if (curMethod == null)
{
curTable = tableiter.next();
continue;
}

while (curMethod)
{
sysDictMethod = SysDictMethod::newTreenodePath(curMethod.treeNodePath());
info(strFmt("%1",sysDictMethod.name()));
curMethod = methodIter.next();
}
}
curTable = tableiter.next();
}
}
}

 Hope this peace of code will help you in working on reflection in Dynamics AX.