[test] Regression test for #58950
[mono.git] / mono / tests / bug-389886-sre-generic-interface-instances.cs
1 using System;
2 using System.Threading;
3 using System.Reflection;
4 using System.Reflection.Emit;
5
6 namespace TestApp
7 {
8     class Program
9     {
10                 static AssemblyBuilder assembly;
11                 static ModuleBuilder module;
12                 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
13
14                 static void SetUp ()
15                 {
16                         AssemblyName assemblyName = new AssemblyName ();
17                         assemblyName.Name = ASSEMBLY_NAME;
18
19                         assembly =
20                                 Thread.GetDomain ().DefineDynamicAssembly (
21                                         assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
22
23                         module = assembly.DefineDynamicModule ("module1", "Module1.dll");
24                 }
25
26                 static int Main()
27                 {
28                         SetUp ();
29
30                         TypeBuilder iface = module.DefineType ("IFace", TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract);
31                         iface.DefineGenericParameters ("T");
32
33                         TypeBuilder parent = module.DefineType ("Parent", TypeAttributes.Public);
34         
35                         TypeBuilder child = module.DefineType ("Child", TypeAttributes.Public, parent);
36
37                         TypeBuilder main = module.DefineType ("Main", TypeAttributes.Public);
38
39                         child.AddInterfaceImplementation (iface.MakeGenericType (new Type [] { typeof(int) }));
40
41                         iface.DefineMethod ("Foo", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(void), Type.EmptyTypes);
42
43                         ConstructorBuilder parent_constructor = parent.DefineDefaultConstructor (MethodAttributes.Public);
44
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);
50
51                         MethodBuilder foo_mb = child.DefineMethod ("Foo", MethodAttributes.Public | MethodAttributes.Virtual, typeof (void), Type.EmptyTypes);
52                         foo_mb.GetILGenerator ().Emit (OpCodes.Ret);
53
54
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);
60
61                         iface.CreateType ();
62
63                         parent.CreateType ();
64
65                         child.CreateType ();
66
67                         Type t = main.CreateType ();
68
69                         MethodInfo method = t.GetMethod ("Main");
70                         method.Invoke (null, null);
71
72                         /*Type gtd = typeof (A<>);
73                         Type oi = gtd.MakeGenericType (gtd.GetGenericArguments ());
74
75                         if (oi != gtd) {
76                                 Console.WriteLine ("fully open instantiation of static type not the same of the generic type definition");
77                                 return 1;
78                         }
79
80                         SetUp ();
81                         TypeBuilder tb = module.DefineType ("Nullable`1", TypeAttributes.Public);
82                         Type[] args = tb.DefineGenericParameters ("T");
83                         Type type = tb.MakeGenericType (args);
84
85                         if (type == tb) {
86                                 Console.WriteLine ("fully open instantiation of TypeBuilder is the same of the TypeBuilder");
87                                 return 2;
88                         }
89                 
90                         Type res = tb.CreateType ();
91                         Type oires = res.MakeGenericType (res.GetGenericArguments ());
92
93                         if (res != oires) {
94                                 Console.WriteLine ("fully open instantiation not the same of the generic type definition for the TypeBuilder created type");
95                                 return 3;
96                         }
97
98                         try {
99                                 type.GetConstructors ();
100                         } catch (Exception e) {
101                                 Console.WriteLine ("fully open instantiation of TypeBuilder must have GetConstructors working {0}", e);
102                                 return 4;
103                         }
104
105
106                         try {
107                                 oires.GetConstructors ();
108                         } catch (Exception e) {
109                                 Console.WriteLine ("fully open instantiation of the TypeBuilder created type must have GetConstructors working {0}", e);
110                                 return 5;
111                         }*/
112
113                         return 0;
114                 }
115         }
116 }