• 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.

    代码
    static void Jimmy_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);
            methodNode     
    = classNode.AOTfindChild(MethodName);

           
    if(!methodNode)
            {
               
    // 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.");
        }
    }
  • 相关阅读:
    Lazarus教程 中文版后续给出
    QBASIC教程
    Object Pascal中文手册 经典教程
    Pascal 基础教程
    Delphi中的关键字与保留字
    Pascal数据结构与算法
    Pascal小游戏 贪吃蛇
    Pascal小游戏 俄罗斯方块怀旧版
    Pascal ASCII和文本的转换
    IDEA安装问题解决
  • 原文地址:https://www.cnblogs.com/Fandyx/p/1758602.html
Copyright © 2020-2023  润新知