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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
//
// MonoGenericClassTest.cs - NUnit Test Cases for MonoGenericClassTest
//
// Rodrigo Kumpera <rkumpera@novell.com>
//
// Copyright (C) 2009 Novell, Inc (http://www.novell.com)
// Copyright 2011 Xamarin Inc (http://www.xamarin.com).
//
#if !MONOTOUCH && !FULL_AOT_RUNTIME
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.IO;
using System.Security;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using NUnit.Framework;
using System.Runtime.CompilerServices;
namespace MonoTests.System.Reflection.Emit
{
[TestFixture]
public class MonoGenericClassTest
{
AssemblyBuilder assembly;
ModuleBuilder module;
int typeCount;
static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.MonoGenericClassTest";
string MakeName ()
{
return "internal__type"+ typeCount++;
}
[SetUp]
public void SetUp ()
{
SetUp (AssemblyBuilderAccess.RunAndSave);
}
void SetUp (AssemblyBuilderAccess access)
{
AssemblyName assemblyName = new AssemblyName ();
assemblyName.Name = ASSEMBLY_NAME;
assembly =
Thread.GetDomain ().DefineDynamicAssembly (
assemblyName, access, Path.GetTempPath ());
module = assembly.DefineDynamicModule ("module1");
typeCount = 0;
}
[Test]
public void TestNameMethods ()
{
TypeBuilder tb = module.DefineType ("foo.type");
tb.DefineGenericParameters ("T", "K");
Type inst = tb.MakeGenericType (typeof (double), typeof (string));
Assert.AreEqual ("type", inst.Name, "#1");
Assert.AreEqual ("foo", inst.Namespace, "#2");
#if !MOBILE
Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]", inst.FullName, "#3");
Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], MonoTests.System.Reflection.Emit.MonoGenericClassTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", inst.AssemblyQualifiedName, "#4");
#elif MOBILE || MOBILE
Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]", inst.FullName, "#3");
Assert.AreEqual ("foo.type[[System.Double, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], MonoTests.System.Reflection.Emit.MonoGenericClassTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", inst.AssemblyQualifiedName, "#4");
Assert.AreEqual ("foo.type[System.Double,System.String]", inst.ToString (), "#5");
#endif
}
static void CheckInst (string prefix, Type inst, int a, int b)
{
var resA = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
var resB = inst.GetMethods (BindingFlags.Public | BindingFlags.Instance);
Assert.AreEqual (a, resA.Length, prefix + 1);
Assert.AreEqual (b, resB.Length, prefix + 2);
}
[Test]
public void MethodsThatRaiseNotSupported ()
{
var tb = module.DefineType ("foo.type");
tb.DefineGenericParameters ("T");
var ginst = tb.MakeGenericType (typeof (double));
try {
ginst.GetElementType ();
Assert.Fail ("#1");
} catch (NotSupportedException) { }
try {
ginst.GetInterface ("foo", true);
Assert.Fail ("#2");
} catch (NotSupportedException) { }
try {
ginst.GetEvent ("foo", BindingFlags.Public | BindingFlags.Instance);
Assert.Fail ("#3");
} catch (NotSupportedException) { }
try {
ginst.GetField ("foo", BindingFlags.Public | BindingFlags.Instance);
Assert.Fail ("#4");
} catch (NotSupportedException) { }
try {
ginst.GetMembers (BindingFlags.Public | BindingFlags.Instance);
Assert.Fail ("#5");
} catch (NotSupportedException) { }
try {
ginst.GetMethod ("Foo");
Assert.Fail ("#6");
} catch (NotSupportedException) { }
try {
ginst.GetNestedType ("foo", BindingFlags.Public | BindingFlags.Instance);
Assert.Fail ("#7");
} catch (NotSupportedException) { }
try {
ginst.GetProperty ("foo");
Assert.Fail ("#8");
} catch (NotSupportedException) { }
try {
var x = ginst.TypeInitializer;
Assert.Fail ("#9");
} catch (NotSupportedException) { }
try {
var x = ginst.InvokeMember ("foo", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, null, null);
Assert.Fail ("#10");
} catch (NotSupportedException) { }
try {
ginst.IsDefined (typeof (int), true);
Assert.Fail ("#11");
} catch (NotSupportedException) { }
try {
ginst.GetCustomAttributes (true);
Assert.Fail ("#12");
} catch (NotSupportedException) { }
try {
ginst.GetCustomAttributes (typeof (int), true);
Assert.Fail ("#13");
} catch (NotSupportedException) { }
try {
ginst.IsAssignableFrom (ginst);
Assert.Fail ("#14");
} catch (NotSupportedException) { }
try {
ginst.GetNestedTypes (BindingFlags.Public);
Assert.Fail ("#14");
} catch (NotSupportedException) { }
}
[Test]
public void ClassMustNotBeRegisteredAfterTypeBuilderIsFinished ()
{
TypeBuilder tb = module.DefineType ("foo.type");
tb.DefineGenericParameters ("T");
var c = tb.CreateType ();
var sreInst = tb.MakeGenericType (typeof (int));
var rtInst = c.MakeGenericType (typeof (int));
Assert.AreNotSame (sreInst, rtInst, "#1");
/*This must not throw*/
rtInst.IsDefined (typeof (int), true);
}
public class Bar<T> {
public class Foo<U> {}
}
[Test]
public void DeclaringTypeMustReturnNonInflatedType ()
{
var ut = new TypeDelegator (typeof (int));
var ut2 = typeof(Bar<>.Foo<>);
var t = ut2.MakeGenericType (ut, ut);
Assert.AreSame (typeof (Bar<>), t.DeclaringType, "#1");
}
public class Base<T> {}
public class SubClass<K> : Base<K> {}
[Test]
public void BaseTypeMustReturnNonInflatedType ()
{
var ut = new TypeDelegator (typeof (int));
var ut2 = typeof(SubClass<>);
var t = ut2.MakeGenericType (ut);
//This is Base<K> where K is SubClass::K
var expected = typeof (Base<>).MakeGenericType (typeof (SubClass<>).GetGenericArguments ()[0]);
Assert.AreSame (expected, t.BaseType, "#1");
}
[Test]
public void GenericClassFromStaleTypeBuilderDoesNotClassInit ()
{
// interface JJJ<T> {
// abstract void W (x : T)
// }
MethodInfo winfo = null;
TypeBuilder ib = null;
Type ic = null;
Type icreated = null;
{
ib = module.DefineType ("Foo.JJJ`1",
TypeAttributes.Public
| TypeAttributes.Interface
| TypeAttributes.Abstract);
String[] gens = { "T" };
GenericTypeParameterBuilder[] gbs = ib.DefineGenericParameters (gens);
var gb = gbs[0];
winfo = ib.DefineMethod ("W",
MethodAttributes.Public |
MethodAttributes.Abstract |
MethodAttributes.Virtual,
CallingConventions.HasThis,
typeof(void),
new Type[] { gb });
icreated = ib.CreateType();
}
// class SSS : JJJ<char> {
// bool wasCalled;
// void JJJ.W (x : T) { wasCalled = true; return; }
// }
TypeBuilder tb = null;
MethodBuilder mb = null;
{
tb = module.DefineType ("Foo.SSS",
TypeAttributes.Public,
null,
new Type[]{ icreated.MakeGenericType(typeof(char)) });
var wasCalledField = tb.DefineField ("wasCalled",
typeof(bool),
FieldAttributes.Public);
mb = tb.DefineMethod ("W_impl",
MethodAttributes.Public | MethodAttributes.Virtual,
CallingConventions.HasThis,
typeof (void),
new Type[] { typeof (char) });
{
var il = mb.GetILGenerator ();
il.Emit (OpCodes.Ldarg_0); // this
il.Emit (OpCodes.Ldc_I4_1);
il.Emit (OpCodes.Stfld, wasCalledField); // this.wasCalled = true
il.Emit (OpCodes.Ret);
}
}
ic = ib.MakeGenericType(typeof (char)); // this is a MonoGenericMethod
var mintf = TypeBuilder.GetMethod(ic, winfo);
// the next line causes mono_class_init_internal () to
// be called on JJJ<char> when we try to setup
// the vtable for SSS
tb.DefineMethodOverride(mb, mintf);
var result = tb.CreateType();
// o = new SSS()
object o = Activator.CreateInstance(result);
Assert.IsNotNull(o, "#1");
// ((JJJ<char>)o).W('a');
var m = icreated.MakeGenericType(typeof(char)).GetMethod("W", BindingFlags.Public | BindingFlags.Instance);
Assert.IsNotNull(m, "#2");
m.Invoke(o, new object[] {'a'});
var f = result.GetField("wasCalled", BindingFlags.Public | BindingFlags.Instance);
Assert.IsNotNull(f, "#3");
var wasCalledVal = f.GetValue(o);
Assert.IsNotNull(wasCalledVal, "#4");
Assert.AreEqual (wasCalledVal.GetType(), typeof(Boolean), "#5");
Assert.AreEqual (wasCalledVal, true, "#6");
}
}
}
#endif
|