Brad
Brad

Reputation: 1369

Dynamics AX Call Non-Static Method Dynamically

I want to make a class that has the ability to dynamically call methods in other classes by name. Ideally, it would accept the class and method names as well as a collection of parameters. dictClass works well for this on static methods, but doesn't seem to work on instance methods.

Is there any way to make the following code work for non-static methods?

[SysEntryPointAttribute]
public str methodExecute(str className, str methodName, str params)
{      
DictClass           dictClass; 
anytype             retVal; 
str                 connMessage;
ExecutePermission   perm; 

perm = new ExecutePermission(); 

// Grants permission to execute the DictClass.callStatic method. 
// DictClass.callStatic runs under code access security. 
perm.assert(); 

dictClass = new DictClass(className2Id(className)); 

if (dictClass != null) 
{        
    retVal = dictClass.callStatic(methodName); 
    connMessage = strfmt("Return value is %1", retVal); 
} 

// Closes the code access permission scope. 
CodeAccessPermission::revertAssert(); 

return connMessage;
}

Upvotes: 2

Views: 7596

Answers (2)

dlannoye
dlannoye

Reputation: 1756

The following code should do the trick. The code I have below can either use a class name or take in an object of the desired type that is already instantiated.

The largest problem was dealing with passing the parameters along. Since there are a variable number I am using a switch statement to pass the correct number of parameters to the method. This doesn't look very elegant, but I am not sure there is another way to accomplish this.

public static anytype callMethod(str _className, str _methodName, container _parameters, Object _object = null)
{
    DictClass dictClass;
    anytype returnValue;
    Object object;
    ExecutePermission permission; 

    // Grants permission to execute the DictClass.callObject method. 
    // DictClass.callObject runs under code access security. 
    permission = new ExecutePermission();
    permission.assert(); 

    if (_object != null)
    {
        dictClass = new DictClass(classidget(_object));
        object = _object;
    }
    else
    {
        dictClass = new DictClass(className2Id(_className));
        object = dictClass.makeObject();
    }

    if (dictClass != null) 
    {
        switch (conLen(_parameters))
        {
            case 0:
                returnValue = dictClass.callObject(_methodName, object);
                break;
            case 1:
                returnValue = dictClass.callObject(_methodName, object, conPeek(_parameters, 1));
                break;
            case 2:
                returnValue = dictClass.callObject(_methodName, object, conPeek(_parameters, 1), conPeek(_parameters, 2));
                break;
             //... Continue this pattern for the number of parameters you need to support.
        }
    }

    // Closes the code access permission scope. 
    CodeAccessPermission::revertAssert();

    return returnValue;
}

Upvotes: 1

Allan Iversen
Allan Iversen

Reputation: 536

static void Job_Example_DictClass_CallObject(Args _args) 
{ 
    DictClass dictClass; 
    anytype   retVal; 
    str      resultOutput; 
    ExecutePermission perm; 

    perm = new ExecutePermission(); 

    // Grants permission to execute the DictClass.callObject method. 
    // DictClass.callObject runs under code access security. 
    perm.assert(); 

    dictClass = new DictClass(classidget(infolog)); 
    if (dictClass != null) 
    { 
        retVal       = dictClass.callObject("toString", infolog); 
        resultOutput = strfmt("Return value is %1", retVal); 
        print resultOutput; 
        pause; 
    } 

    // Closes the code access permission scope. 
    CodeAccessPermission::revertAssert(); 
}

Reference to MSDN

Also try looking into DictClass.MakeObject:

Reference to MSDN (MakeObject)

Upvotes: 1

Related Questions