Create class from code

March 11, 2010 No comment

Did you know that you can build/modify classes from code? It is actually not so hard, just use the ClassBuild-class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static void JeDoe_testClassBuild(Args _args)
{
    ClassBuild  classBuild;
    ClassNode   classNode;
    Str         myCode;
    ;
    myCode =
@"
static void Main(Args _args)
{
    ;
    info('Hello World!');
}
";
 
    classBuild = new ClassBuild("MyTest", false);
    classBuild.addMethod("Main", myCode);
 
    classNode = classBuild.classNode();
    classNode.AOTcompile();
    classNode.AOTrun();
}

The sample above will create a new class MyTest with a ‘Hello World’ Main-method and will actually save compile/save the code in the AOT and then run it. Cool?

Note: The SysDictClass/SysDictMethod-class can also help you creating proper classes and methods. Maybe I’ll blog about this later.