Reputation: 1369
Is it possible to write an x++ (or C# CLR) method that dynamically creates a new class object and/or methods in the AOT?
makeCustomClass(string className)
{
//create new class with name className;
//populate methods into that class;
}
I'm not even sure where to start on this one, so some sample code would be a big help.
Upvotes: 2
Views: 3732
Reputation: 3097
You have good examples on the Product Builder classes (PBA prefix) as PB creates some classes for the models during compilation process.
Upvotes: 1
Reputation: 1369
Here is the solution:
static void MakeClass(Args _args)
{
ClassBuild classBuild;
DictClass dictClass;
;
classBuild = new ClassBuild("TRN_ClassBuild", false);
classBuild.addMethod("test",
@"void test()
{
;
print 'Hello';
pause;
}");
classBuild.addMethod("test2", 'void test2()\n{\n}');
classBuild.addSourceToMethod("test2", @"
str testVar;
;
testVar = 'TRN_ClassBuild';
info('We created a Class ' + testVar + ' and can use its methods.');
TreeNode::findNode('Classes\\'+TestVar).AotNewWindow();");
classBuild.classNode().AOTcompile();
box::info(strFmt("Creating class %1 with 2 methods. Code of method test2:\n\n%2", classBuild.name(),
classBuild.getMethodImplementation("test2", false).AOTgetSource()));
dictClass = new DictClass(className2Id(classBuild.name()));
dictClass.callObject('test2', dictClass.makeObject());
}
Plagiarized from the following article, then uplifted to work in AX 2012 R2. http://kashperuk.blogspot.com/2006/11/today-i-want-to-write-about-using-class.html
Upvotes: 7