2 using System
.Threading
;
3 using System
.Reflection
;
4 using System
.Reflection
.Emit
;
10 static AssemblyBuilder assembly
;
11 static ModuleBuilder module
;
12 static string ASSEMBLY_NAME
= "MonoTests.System.Reflection.Emit.TypeBuilderTest";
16 AssemblyName assemblyName
= new AssemblyName ();
17 assemblyName
.Name
= ASSEMBLY_NAME
;
20 Thread
.GetDomain ().DefineDynamicAssembly (
21 assemblyName
, AssemblyBuilderAccess
.RunAndSave
, ".");
23 module
= assembly
.DefineDynamicModule ("module1", "Module1.dll");
30 TypeBuilder iface
= module
.DefineType ("IFace", TypeAttributes
.Public
| TypeAttributes
.Interface
| TypeAttributes
.Abstract
);
31 iface
.DefineGenericParameters ("T");
33 TypeBuilder parent
= module
.DefineType ("Parent", TypeAttributes
.Public
);
35 TypeBuilder child
= module
.DefineType ("Child", TypeAttributes
.Public
, parent
);
37 TypeBuilder main
= module
.DefineType ("Main", TypeAttributes
.Public
);
39 child
.AddInterfaceImplementation (iface
.MakeGenericType (new Type
[] { typeof(int) }
));
41 iface
.DefineMethod ("Foo", MethodAttributes
.Public
| MethodAttributes
.Abstract
| MethodAttributes
.Virtual
, typeof(void), Type
.EmptyTypes
);
43 ConstructorBuilder parent_constructor
= parent
.DefineDefaultConstructor (MethodAttributes
.Public
);
45 ConstructorBuilder ctor
= child
.DefineConstructor (MethodAttributes
.Public
, CallingConventions
.Standard
, Type
.EmptyTypes
);
46 ILGenerator ig
= ctor
.GetILGenerator ();
47 ig
.Emit (OpCodes
.Ldarg_0
);
48 ig
.Emit (OpCodes
.Call
, parent_constructor
);
49 ig
.Emit (OpCodes
.Ret
);
51 MethodBuilder foo_mb
= child
.DefineMethod ("Foo", MethodAttributes
.Public
| MethodAttributes
.Virtual
, typeof (void), Type
.EmptyTypes
);
52 foo_mb
.GetILGenerator ().Emit (OpCodes
.Ret
);
55 MethodBuilder main_mb
= main
.DefineMethod ("Main", MethodAttributes
.Public
| MethodAttributes
.Static
, typeof (void), Type
.EmptyTypes
);
56 ig
= main_mb
.GetILGenerator ();
57 ig
.Emit (OpCodes
.Newobj
, ctor
);
58 ig
.Emit (OpCodes
.Callvirt
, foo_mb
);
59 ig
.Emit (OpCodes
.Ret
);
67 Type t
= main
.CreateType ();
69 MethodInfo method
= t
.GetMethod ("Main");
70 method
.Invoke (null, null);
72 /*Type gtd = typeof (A<>);
73 Type oi = gtd.MakeGenericType (gtd.GetGenericArguments ());
76 Console.WriteLine ("fully open instantiation of static type not the same of the generic type definition");
81 TypeBuilder tb = module.DefineType ("Nullable`1", TypeAttributes.Public);
82 Type[] args = tb.DefineGenericParameters ("T");
83 Type type = tb.MakeGenericType (args);
86 Console.WriteLine ("fully open instantiation of TypeBuilder is the same of the TypeBuilder");
90 Type res = tb.CreateType ();
91 Type oires = res.MakeGenericType (res.GetGenericArguments ());
94 Console.WriteLine ("fully open instantiation not the same of the generic type definition for the TypeBuilder created type");
99 type.GetConstructors ();
100 } catch (Exception e) {
101 Console.WriteLine ("fully open instantiation of TypeBuilder must have GetConstructors working {0}", e);
107 oires.GetConstructors ();
108 } catch (Exception e) {
109 Console.WriteLine ("fully open instantiation of the TypeBuilder created type must have GetConstructors working {0}", e);