• 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.");
        }
    }
  • 相关阅读:
    MyBatis入门基础
    复制复杂链表
    二叉树中和为某一值的所有路径
    树的层次遍历
    Statement, PreparedStatement和CallableStatement的区别
    JSP有哪些动作?
    latex 输入矩阵
    Struts简单入门实例
    在Eclipse里面配置Struts2
    Windows使用Github
  • 原文地址:https://www.cnblogs.com/Fandyx/p/1758602.html
Copyright © 2020-2023  润新知