Tuesday, December 20, 2011

Run static method through X++ code

The following is code that will allow you to run a static method using two strings and a container. You need to assert and revert permissions around doing this but it allows you to dynamically run a static method without knowing what the method is.  This is good for frameworks.
  • _className     = the string name of the AX AOT Class name.
  • _methodName = the string name of the method from the AX AOT Class above.
  • _params           = the container which has the variables, [parameter 1, parameter 2, etc]. 
// Start DAX code
// From parameters:
ClassName _className
MethodName _methodName
Container _params;
// From method variables
Container               result;
DictClass               dictClass;
;

// This code will run the static method
executePermission = new ExecutePermission();
executePermission.assert();

dictClass = new DictClass(className2Id(_className));
result = dictClass.callStatic(_methodName, _params);

CodeAccessPermission::revertAssert();
// End DAX code

This code is dynamic and powerful.  Enjoy!

1 comment: