I want to add a method dynamically to a component in ax 2012, how can I do this through code? Is it possible?
Add a method dynamically in ax 2012 component
2.6k Views Asked by user3226663 At
2
There are 2 best solutions below
0
On
private void findOrCreateTimeStamp(
SysVersionControlTmpItem _item)
{
str timeStamp;
UtilElements utilElement;
SysDictClass dictClass;
ClassBuild classBuild;
SysVersionControlTmpItem item = _item;
str methodName = "csGetVersion";
int time = timenow();
TreeNode treeNode = TreeNode::findNode(item.ItemPath);
utilElement = treeNode.utilElement();
timeStamp = date2Str(
today(),
321,
DateDay::Digits2,
DateSeparator::Slash,
DateMonth::Digits2,
DateSeparator::Slash,
DateYear::Digits4,
DateFlags::None);
timeStamp = timeStamp + "_" +
num2str0(time div 3600, 2, 0, 0, 0) + "_" +
num2Str0(time mod 3600 div 60, 2, 0, 0, 0) + "_" +
num2Str0(time mod 3600 mod 60, 2, 0, 0, 0);
if (utilElement.recordType == UtilElementType::Class)
{
dictClass = new SysDictClass(className2Id(utilElement.name));
classBuild = new ClassBuild(utilElement.name, true);
if (dictClass.hasStaticMethod(methodName))
{
//Override method here, since the method already exists in the component.
classBuild.overrideMethod(methodName,
@"public static str csGetVersion()" +
"{" +
"return '" + timeStamp + "';" +
"}");
}
else
{
//Make a new method here since it does'nt exist in the component.
classBuild.addMethod(methodName,
@"public static str csGetVersion()" +
"{" +
"return '" + timeStamp + "';" +
"}");
}
classBuild.classNode().AOTcompile();
}
}
The above method is created by referring to previous answer and other professionals. It validates if the method exists in the class first. If found it overrides else creates new method. For other elements like Tables, Forms and Maps we can implement in the similar way.
Here is a job I wrote that demonstrates a bunch of different ways of doing what you're wanting: