在ax 2012组件中动态添加方法

时间:2015-04-24 06:28:16

标签: axapta dynamics-ax-2012 dynamics-ax-2012-r2 dynamic-method

我想在ax 2012中动态添加一个方法,如何通过代码实现?有可能吗?

2 个答案:

答案 0 :(得分:3)

这是我写的一份工作,展示了一系列不同的方式来做你想要的事情:

static void Job79(Args _args)
{
    TreeNode        treeNode = TreeNode::findNode(@'\Classes\Activities');
    SysDictClass    sysDictClass = new SysDictClass(treeNode.applObjectId());
    FormRun         formRun = new FormRun(new Args(formStr(AifAction)));
    Form            form = new Form(formStr(AifAction));
    ClassBuild      classBuild;
    SysDictTable    sysDictTable = new SysDictTable(tableNum(AccountSumMap)); // Maps are treated like tables
    SysDictMethod   sysDictMethod;
    int             i;
    MemberFunction  method;
    str             methodSource = 'public static str getTime()\n{\n\treturn "3/3/2015";\n}';


    // Find if class has a method
    if (sysDictClass.hasObjectMethod('delete'))
    {
        info("Found object method delete");
    }

    if (sysDictClass.hasStaticMethod('main'))
    {
        info("Found static method main");
    }

    // Find if form has a method
    if (formHasMethod(formRun, 'init'))
    {
        info("Found form method 'init'");
    }


    if (form.AOTfindChild('methods').AOTfindChild('refreshGrid') != null)
    {
        info("Found 'refreshGrid' method on AifAction");
    }


    if (sysDictClass.hasStaticMethod('getTime') == false)
    {
        classBuild = new ClassBuild(sysDictClass.name());

        treeNode = classBuild.addMethod('getTime', methodSource);

        if (classBuild.classNode().AOTcompile())
        {
            classBuild.classNode().AOTsave();
            info("Added method getTime, compiled, and saved");
        }
        else
        {
            info(strFmt("Unable to compile method 'getTime' with source code '%1', restoring class...", treeNode.AOTgetSource()));
            // Delete the non-compiling method
            if (treeNode)
                treeNode.AOTdelete();

            classBuild.classNode().AOTsave();
        }

    }
    else
    {
        info("Method 'getTime' already exists");
    }

    if (sysDictTable.isMap())
    {
        if (sysDictTable.doesMethodExist('getTime') == false)
        {
            treeNode = sysDictTable.treeNode().AOTfindChild('methods').AOTadd('getTime');
            method = sysDictTable.treeNode().AOTfindChild('methods').AOTfindChild('getTime');
            method.AOTsave();
            method.AOTsetSource(methodSource, true);
            method.AOTsave();


            if (sysDictTable.treeNode().AOTcompile())
            {
                sysDictTable.treeNode().AOTsave();
                info(strFmt("Added 'getTime' to AccountSumMap"));
            }
            else
            {
                info(strFmt("Unable to compile method 'getTime' with source code '%1', restoring class...", treeNode.AOTgetSource()));

                // Delete the non-compiling method
                if (treeNode)
                    treeNode.AOTdelete();

                sysDictTable.treeNode().AOTsave();
            }
        }
    }
}

答案 1 :(得分:1)

<?php if(isset($user['first_name'])) { echo $user['first_name']; } ?>

上述方法是参考之前的答案和其他专业人士创建的。它首先验证方法是否存在于类中。如果发现它覆盖其他创建新方法。对于其他元素,如表格,表格和地图,我们可以以类似的方式实现。