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.");
        }
    }
  • 相关阅读:
    SQL Server 错误15401:没有找到Windows NT用户或组‘EMLE\ASPNET’
    使用 TestContext 类
    C# 动态2维数组
    2010年春季 软件测试技术 软件项目管理 实验安排
    代码覆盖率结果:没有为此测试运行启用代码覆盖率
    InternetGetCookie/InternetSetCookie (WinInet) changed with Internet Explorer 7
    C# 替换文本文件中的某一行 (要求此文件存在)
    android 复制 粘贴 功能实现
    java动态代理(JDK和cglib)
    使用ant打web应用更新包
  • 原文地址:https://www.cnblogs.com/Fandyx/p/1758602.html
Copyright © 2011-2022 走看看