zoukankan      html  css  js  c++  java
  • 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.");
        }
    }
  • 相关阅读:
    博客
    NFS服务配置
    LAMP环境搭建与配置
    Linux系统管理技巧(下)
    Linux系统管理技巧(上)
    LVM
    安装RPM包或源码包
    Linux第四章 文件和目录管理
    MySQL主从配置 : 配置主和从(一) 配置主
    MySQL主从配置:主从介绍
  • 原文地址:https://www.cnblogs.com/Fandyx/p/1758602.html
Copyright © 2011-2022 走看看