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