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.");
        }
    }
  • 相关阅读:
    linux ---性能监控(工具)
    inux --- 服务器性能监控
    Jmeter——BeanShell PreProcessor的用法
    Jmete ----r默认报告优化
    jmeter --- 基于InfluxDB&Grafana的JMeter实时性能测试数据的监控和展示
    转Jmeter报告优化之New XSL stylesheet
    Jmeter----组件执行顺序与作用域
    Jmeter----属性和变量
    Jmeter----逻辑控制器(Logic Controller)
    Jmeter----HTTP Request Defaults
  • 原文地址:https://www.cnblogs.com/Fandyx/p/1758602.html
Copyright © 2011-2022 走看看