2 using System
.Reflection
;
3 using System
.Reflection
.Emit
;
7 public static int Main() {
8 AssemblyBuilder abuilder
;
9 ModuleBuilder mbuilder
;
11 FieldBuilder fbuilder
;
12 PropertyBuilder pbuilder
;
14 String name
= "tcgen.exe";
15 MethodBuilder method
, get_method
;
16 TypeAttributes attrs
= TypeAttributes
.Public
| TypeAttributes
.Class
;
17 MethodAttributes mattrs
= MethodAttributes
.Public
| MethodAttributes
.Static
;
18 byte[] body
= {0x16, 0x2a}
; // ldc.i4.0 ret
20 an
= new AssemblyName ();
22 abuilder
= AppDomain
.CurrentDomain
.DefineDynamicAssembly (an
, AssemblyBuilderAccess
.Save
);
24 mbuilder
= abuilder
.DefineDynamicModule (name
, name
);
26 tbuilder
= mbuilder
.DefineType ("Test.CodeGen", attrs
);
27 Type result
= typeof(int);
28 Type
[] param
= new Type
[] {typeof (String[])}
;
29 method
= tbuilder
.DefineMethod("Main", mattrs
, result
, param
);
30 method
.CreateMethodBody (body
, body
.Length
);
32 fbuilder
= tbuilder
.DefineField ("int_field", typeof(int), FieldAttributes
.Private
);
33 fbuilder
= tbuilder
.DefineField ("string_field", typeof(string), FieldAttributes
.Public
);
34 /*pbuilder = tbuilder.DefineProperty ("FieldI", PropertyAttributes.None, typeof(int), null);
35 get_method = tbuilder.DefineMethod("get_FieldI", MethodAttributes.Public, result, null);
36 get_method.CreateMethodBody (body, body.Length);
37 pbuilder.SetGetMethod (get_method);*/
39 Type t
= tbuilder
.CreateType ();
40 abuilder
.SetEntryPoint (method
);
42 Console
.WriteLine ("abuilder == module.assembly: {0}", abuilder
== mbuilder
.Assembly
);
43 Console
.WriteLine ("abuilder == type.assembly: {0}", abuilder
== t
.Assembly
);
44 Console
.WriteLine ("abuilder == tbuilder.assembly: {0}", abuilder
== tbuilder
.Assembly
);
45 Console
.WriteLine ("mbuilder == type.Module: {0}", mbuilder
== t
.Module
);
46 Console
.WriteLine ("mbuilder == tbuilder.Module: {0}", mbuilder
== tbuilder
.Module
);