Tuesday, 14 October 2014

AX 2012: Creating parm methods automatically

static void createParmMethod(Args _args)
{
    #AOT

    ClassName className = classStr(MyClass);

    TreeNode classDeclarationTreeNode;
    TreeNode classTreeNode;
    TreeNode parmMethodNode;

    Source classDeclaration;

    System.Text.RegularExpressions.MatchCollection mcVariables;
    System.Text.RegularExpressions.Match mVariable;
    int matchCount;
    int matchIdx;

    System.Text.RegularExpressions.GroupCollection gcVariableDeclaration;
    System.Text.RegularExpressions.Group gVariableDeclarationPart;

    str variableType;
    str variableName;

    str pattern = ' (?[a-zA-Z0-9_]+) (?[a-zA-Z0-9_]+);';

    Source parmMethodBody;

    classTreeNode = TreeNode::findNode(strFmt(@"%1\%2", #ClassesPath, className));

    classDeclarationTreeNode = TreeNode::findNode(
        strFmt(@"%1\%2\ClassDeclaration",
        #ClassesPath,
        className));

    classDeclaration = classDeclarationTreeNode.AOTgetSource();

    mcVariables = System.Text.RegularExpressions.Regex::Matches(
        classDeclaration,
        pattern,
        System.Text.RegularExpressions.RegexOptions::Singleline);
    matchCount = CLRInterop::getAnyTypeForObject(mcVariables.get_Count());

    for (matchIdx = 0; matchIdx < matchCount; matchIdx++)
    {
        mVariable = mcVariables.get_Item(matchIdx);
        gcVariableDeclaration = mVariable.get_Groups();

        gVariableDeclarationPart = gcVariableDeclaration.get_Item('VarType');
        variableType = gVariableDeclarationPart.get_Value();

        gVariableDeclarationPart = gcVariableDeclaration.get_Item('VarName');
        variableName = gVariableDeclarationPart.get_Value();

        parmMethodBody = new xppSource().parmMethod(variableType, variableName);

        parmMethodNode = classTreeNode.AOTadd('method1');
        parmMethodNode.AOTsetSource(parmMethodBody);
        classTreeNode.AOTsave();
    }

    classTreeNode.AOTcompile();
}
for more info please look :http://dynamicsaxinsight.wordpress.com/2014/10/10/ax-2012-creating-parm-methods-automatically/

 

No comments:

Post a Comment

intercompany PO multiple product receipt by x++

public void processStampICPO(PackingSlipId _deliveryNote,                             Transdate _deliverydate,                             ...