2 using System
.Threading
;
3 using System
.Reflection
;
4 using System
.Reflection
.Emit
;
10 static AssemblyBuilder assembly
;
11 static ModuleBuilder module
;
13 static ConstructorBuilder delegate_ctor
;
14 static MethodBuilder invoke_mb
;
15 static string ASSEMBLY_NAME
= "MonoTests.System.Reflection.Emit.TypeBuilderTest";
19 AssemblyName assemblyName
= new AssemblyName ();
20 assemblyName
.Name
= ASSEMBLY_NAME
;
23 Thread
.GetDomain ().DefineDynamicAssembly (
24 assemblyName
, AssemblyBuilderAccess
.RunAndSave
, ".");
26 module
= assembly
.DefineDynamicModule ("module1", "bla.exe");
29 static TypeBuilder
DefineDelegate () {
30 TypeBuilder typeBuilder
= module
.DefineType( "MyDelegate",
31 TypeAttributes
.Public
| TypeAttributes
.Class
| TypeAttributes
.Sealed
,
33 args
= typeBuilder
.DefineGenericParameters ("TIn", "TOut");
35 delegate_ctor
= typeBuilder
.DefineConstructor(
36 MethodAttributes
.Public
| MethodAttributes
.HideBySig
|
37 MethodAttributes
.RTSpecialName
| MethodAttributes
.SpecialName
,
38 CallingConventions
.Standard
,
39 new Type
[] { typeof(Object), typeof (IntPtr) }
);
41 delegate_ctor
.SetImplementationFlags( MethodImplAttributes
.Runtime
| MethodImplAttributes
.Managed
);
43 invoke_mb
= typeBuilder
.DefineMethod(
45 MethodAttributes
.Public
| MethodAttributes
.Virtual
| MethodAttributes
.HideBySig
,
47 new Type
[] { args [0] }
);
49 invoke_mb
.SetImplementationFlags( MethodImplAttributes
.Runtime
| MethodImplAttributes
.Managed
);
51 MethodBuilder mb
= typeBuilder
.DefineMethod(
53 MethodAttributes
.Public
| MethodAttributes
.Virtual
| MethodAttributes
.HideBySig
,
54 typeof (IAsyncResult
),
55 new Type
[] { args [0], typeof (AsyncCallback), typeof (object) }
);
57 mb
.SetImplementationFlags( MethodImplAttributes
.Runtime
| MethodImplAttributes
.Managed
);
59 mb
= typeBuilder
.DefineMethod(
61 MethodAttributes
.Public
| MethodAttributes
.Virtual
| MethodAttributes
.HideBySig
,
63 new Type
[] { typeof (IAsyncResult) }
);
65 mb
.SetImplementationFlags( MethodImplAttributes
.Runtime
| MethodImplAttributes
.Managed
);
74 TypeBuilder tb
= DefineDelegate ();
75 TypeBuilder main
= module
.DefineType ("Main", TypeAttributes
.Public
);
76 /* >>>move this to after the SetParent call and things will work<<< */
77 Type inst
= tb
.MakeGenericType (new Type
[] {typeof (double), typeof(int)}
);
79 tb
.SetParent (typeof( System
.MulticastDelegate
));
81 ConstructorBuilder ctor
= main
.DefineDefaultConstructor (MethodAttributes
.Public
);
83 MethodBuilder foo_mb
= main
.DefineMethod("Foo", MethodAttributes
.Public
, typeof (int), new Type
[] { typeof (double) }
);
84 ILGenerator ig
= foo_mb
.GetILGenerator ();
85 ig
.Emit (OpCodes
.Ldc_I4_0
);
86 ig
.Emit (OpCodes
.Ret
);
89 MethodBuilder main_mb
= main
.DefineMethod ("Main", MethodAttributes
.Public
| MethodAttributes
.Static
, typeof (int), Type
.EmptyTypes
);
90 ig
= main_mb
.GetILGenerator ();
92 ig
.Emit (OpCodes
.Newobj
, ctor
);
93 ig
.Emit (OpCodes
.Ldftn
, foo_mb
);
94 ig
.Emit (OpCodes
.Newobj
, TypeBuilder
.GetConstructor (inst
, delegate_ctor
));
95 ig
.Emit (OpCodes
.Ldc_R8
, 2.2);
96 ig
.Emit (OpCodes
.Callvirt
, TypeBuilder
.GetMethod (inst
, invoke_mb
));
98 ig
.Emit (OpCodes
.Ret
);
102 Type t
= main
.CreateType ();
104 MethodInfo method
= t
.GetMethod ("Main");
105 method
.Invoke (null, null);