2010-04-19 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / tests / bug-389886-sre-generic-interface-instances.cs
blobcd9ca8c7ba07ab0799952e3177d781253a1b70d8
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 string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
14 static void SetUp ()
16 AssemblyName assemblyName = new AssemblyName ();
17 assemblyName.Name = ASSEMBLY_NAME;
19 assembly =
20 Thread.GetDomain ().DefineDynamicAssembly (
21 assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
23 module = assembly.DefineDynamicModule ("module1", "Module1.dll");
26 static int Main()
28 SetUp ();
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);
61 iface.CreateType ();
63 parent.CreateType ();
65 child.CreateType ();
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 ());
75 if (oi != gtd) {
76 Console.WriteLine ("fully open instantiation of static type not the same of the generic type definition");
77 return 1;
80 SetUp ();
81 TypeBuilder tb = module.DefineType ("Nullable`1", TypeAttributes.Public);
82 Type[] args = tb.DefineGenericParameters ("T");
83 Type type = tb.MakeGenericType (args);
85 if (type == tb) {
86 Console.WriteLine ("fully open instantiation of TypeBuilder is the same of the TypeBuilder");
87 return 2;
90 Type res = tb.CreateType ();
91 Type oires = res.MakeGenericType (res.GetGenericArguments ());
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;
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;
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;
113 return 0;