TypeBuilder
, MethodBuilder
, etc are Type
, MethodInfo
, but they dont have all abilities of Type
, MethodInfo
until you call TypeBuilder.CreateType()
. The reason TypeBuilder
is derived from Type
is it allows you you while building class A
and B
, you can reference they in both directions without finishing them. Let me give an example:
// sample.x
class A {
void method1(B b) {}
}
class B {
void method2(A a) {}
}
So the C# code to generate class A
and B
would be:
// ...
// define classes
TypeBuilder classA = moduleBuilder.DefineType("A");
TypeBuilder classB = moduleBuilder.DefineType("B");
// define class A's methods
MethodBuilder method1 = classA.defineMethod("method1", MethodAttributes.Public);
method1.SetParameters(classB);
// ... build method body
// define class B's methods
MethodBuilder method2 = classB.defineMethod("method2", MethodAttributes.Public);
method1.SetParameters(classA);
// ... build method body
// finish classes
classA.CreateType();
classB.CreateType();
// this time you can use GetMethod but you can not modify classes any more.
Answer your questions, there are no ways to get list of methods, properties, etc in TypeBuilder
until you call CreateType
. But you should remember these methods, properties are created by your code, so you should know them all. If you want to create some class TypeBuilderWrapper
, it's your choice. But from my opinion, you should do something like:
- Write your own models (
XClass
,XMethod
,XParam
, etc) for you X language. - Turn your
XParser
to parse the X files into language model objects. - Do some analysis on created models to create links between your models. For example: in
sample.x
above, inB.method2
, parameterA a
should have a link toclass A
model. - using
Reflection.Emit
to create the target assembly. Remember the order is: define classes, define classes' methods, define classes methods' body, etc. Then callCreateType
to finish then all. The order can be changed depends on your language design.
Those are all my idea, the code might not work. When I created my simple-stupid language, I created somethings like those. Hope I could help you.