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, in B.method2
, parameter A a
should have a link to class 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 call CreateType
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.
CreateType()
before trying to useGetMethod()
? Also, could you explain why do you need overload resolution? When generating code, you usually already know which method you want to call. – SamalaTypeBuilder.CreateType()
has been invoked, because it finalizes the type and loads in into memory. I haven't tested it myself though. – Upshotclass A { }
andclass B : A { }
. If my method has a signature ofvoid test(A a)
, I must be able to call it passing an instance of classB
. – UpshotB
and you know you want to call a method called"test"
. But couldn't you change your code so that you know you want to callvoid test(A a)
? In other words, can you use theMethodInfo
to call the method, instead ofstring
? – Samalastring
into aMethodBuilder
is mostly what making a compiler is all about! When I parse the input string containing the source code, I don't have MethodBuilders. I can only say that there's a method calledtest
being invoked and something has been passed to it as a parameter, and it turns out to be an object of classB
. – Upshot((dynamic)(new object())).ToString()
and see for yourself in the Reflector. – Upshot