Create class and methods in x++

This job does three things.

1. Creates a new class, compiles and saves it.

2. Finds the class and adds a new static method to it.

3. Calls the new method using DictClass.

If you want to you can just add to the newClassBuld.classNode() instead of finding it again as I do in this example. I did it this way to show both how to create a new class and how to add methods to an existing class.

This job was created in Dynamics AX 4.0 and has not been tested in any other version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
static void FO_CreateClassMethod(Args _args)
{
    #AOT
    TreeNode                classesRootNode;
    TreeNode                methodNode;
    ClassNode               classNode;
    str                     MethodContent;
    ClassBuild              newClassBuild;
    ClassName               className = "HelloWorld";
    str                     MethodName  = "sayHelloWorld";
    str                     classDeclaration;
    ClassId                 classId;
    DictClass               dictClass;
    ;
 
// ---------------- Step one
 
    // Create a new classBuild based on the new class.
    newClassBuild = new ClassBuild(className);
 
    // Save and compile the class.
    newClassBuild.classNode().AOTcompile();
    newClassBuild.classNode().AOTsave();
 
    classId = newClassBuild.classNode().iD();
 
// ---------------- Step two
 
    // Find the AOT class node using the AOT macro
    classesRootNode = TreeNode::findNode(#ClassesPath);
 
    // Find the class we want to add to
    classNode = classesRootNode.AOTfindChild(className);
 
    if (classNode)
    {
 
        MethodContent = 'static void %1()\n' +
        '{;\n\n' +
        '    info("Hello World!");\n\n' +
        '}\n';
 
        // Add method name to the methodContent.
        MethodContent = strFmt(MethodContent, MethodName);
 
        // Add the method to the class.
        classNode.AOTadd(MethodName);
 
        // Find new method.
        methodNode  = classNode.AOTfindChild(MethodName);
 
        // Add method content.
        methodNode.AOTsetSource(MethodContent, true);
 
        // Compile, save the class and refresh the AOT.
        classNode.AOTcompile();
        classNode.AOTsave();
        classNode.AOTrefresh();
 
        // -------- Step three
 
        // Find the class
        dictClass = new DictClass(classId);
 
        // Call the new static method.
        dictClass.callStatic(MethodName);
    }
    else
    {
        info("New class was not found.");
    }
}

Last 5 posts in Development

3 thoughts on “Create class and methods in x++

  1. Just add this on line 20:

    newClassBuild.addMethod(‘classdeclaration’, ‘public class ‘ + className + ‘ extends FormLetter \n{\n}\n’);

    This makes the class extend from FormLetter, maybe a bad example, but I think you get the point. Just change the “FormLetter” part to the class you want to extend.

    What this means is that if you don´t add a classdeclaration, the ClassBuild class will create a default classdeclaration for you. If you don´t want this, add it manually like any other method.

    // Erik

Leave a Reply

Your email address will not be published. Required fields are marked *