Revert "[2018-06] [System]: `MonoTlsStream` is now `IDisposable` and cleans up on...
[mono-project.git] / mono / tests / bug-389886-3.cs
blob33b401986ba23d8c6a817f30f7a44078a9b21ab4
1 using System;
2 using System.Threading;
3 using System.Reflection;
4 using System.Reflection.Emit;
6 namespace TestApp
8 class Program
10 static AssemblyBuilder assembly;
11 static ModuleBuilder module;
12 static Type[] args;
13 static ConstructorBuilder delegate_ctor;
14 static MethodBuilder invoke_mb;
15 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
17 static void SetUp ()
19 AssemblyName assemblyName = new AssemblyName ();
20 assemblyName.Name = ASSEMBLY_NAME;
22 assembly =
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,
32 typeof (object) );
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(
44 "Invoke",
45 MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig,
46 args [1],
47 new Type[] { args [0] } );
49 invoke_mb.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed );
51 MethodBuilder mb = typeBuilder.DefineMethod(
52 "BeginInvoke",
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(
60 "EndInvoke",
61 MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig,
62 args [1],
63 new Type[] { typeof (IAsyncResult) } );
65 mb.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed );
67 return typeBuilder;
70 static int Main()
72 SetUp ();
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);
100 tb.CreateType ();
102 Type t = main.CreateType ();
104 MethodInfo method = t.GetMethod ("Main");
105 method.Invoke (null, null);
107 return 0;