| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Diagnostics; |
| 4 | using System.IO; |
| 5 | using SR = System.Reflection; |
| 6 | using System.Runtime.CompilerServices; |
| 7 | |
| 8 | using Mono.Cecil.Cil; |
| 9 | |
| 10 | using NUnit.Framework; |
| 11 | |
| 12 | namespace Mono.Cecil.Tests { |
| 13 | |
| 14 | [TestFixture] |
| 15 | public class ImportReflectionTests : BaseTestFixture { |
| 16 | |
| 17 | [Test] |
| 18 | public void ImportString () |
| 19 | { |
| 20 | var get_string = Compile<Func<string>> ((_, body) => { |
| 21 | var il = body.GetILProcessor (); |
| 22 | il.Emit (OpCodes.Ldstr, "yo dawg!"); |
| 23 | il.Emit (OpCodes.Ret); |
| 24 | }); |
| 25 | |
| 26 | Assert.AreEqual ("yo dawg!", get_string ()); |
| 27 | } |
| 28 | |
| 29 | [Test] |
| 30 | public void ImportInt () |
| 31 | { |
| 32 | var add = Compile<Func<int, int, int>> ((_, body) => { |
| 33 | var il = body.GetILProcessor (); |
| 34 | il.Emit (OpCodes.Ldarg_0); |
| 35 | il.Emit (OpCodes.Ldarg_1); |
| 36 | il.Emit (OpCodes.Add); |
| 37 | il.Emit (OpCodes.Ret); |
| 38 | }); |
| 39 | |
| 40 | Assert.AreEqual (42, add (40, 2)); |
| 41 | } |
| 42 | |
| 43 | [Test] |
| 44 | public void ImportStringByRef () |
| 45 | { |
| 46 | var get_string = Compile<Func<string, string>> ((module, body) => { |
| 47 | var type = module.Types [1]; |
| 48 | |
| 49 | var method_by_ref = new MethodDefinition { |
| 50 | Name = "ModifyString", |
| 51 | IsPrivate = true, |
| 52 | IsStatic = true, |
| 53 | }; |
| 54 | |
| 55 | type.Methods.Add (method_by_ref); |
| 56 | |
| 57 | method_by_ref.MethodReturnType.ReturnType = module.Import (typeof (void)); |
| 58 | |
| 59 | method_by_ref.Parameters.Add (new ParameterDefinition (module.Import (typeof (string)))); |
| 60 | method_by_ref.Parameters.Add (new ParameterDefinition (module.Import (typeof (string).MakeByRefType ()))); |
| 61 | |
| 62 | var m_il = method_by_ref.Body.GetILProcessor (); |
| 63 | m_il.Emit (OpCodes.Ldarg_1); |
| 64 | m_il.Emit (OpCodes.Ldarg_0); |
| 65 | m_il.Emit (OpCodes.Stind_Ref); |
| 66 | m_il.Emit (OpCodes.Ret); |
| 67 | |
| 68 | var v_0 = new VariableDefinition (module.Import (typeof (string))); |
| 69 | body.Variables.Add (v_0); |
| 70 | |
| 71 | var il = body.GetILProcessor (); |
| 72 | il.Emit (OpCodes.Ldnull); |
| 73 | il.Emit (OpCodes.Stloc, v_0); |
| 74 | il.Emit (OpCodes.Ldarg_0); |
| 75 | il.Emit (OpCodes.Ldloca, v_0); |
| 76 | il.Emit (OpCodes.Call, method_by_ref); |
| 77 | il.Emit (OpCodes.Ldloc_0); |
| 78 | il.Emit (OpCodes.Ret); |
| 79 | }); |
| 80 | |
| 81 | Assert.AreEqual ("foo", get_string ("foo")); |
| 82 | } |
| 83 | |
| 84 | [Test] |
| 85 | public void ImportStringArray () |
| 86 | { |
| 87 | var identity = Compile<Func<string [,], string [,]>> ((module, body) => { |
| 88 | var il = body.GetILProcessor (); |
| 89 | il.Emit (OpCodes.Ldarg_0); |
| 90 | il.Emit (OpCodes.Ret); |
| 91 | }); |
| 92 | |
| 93 | var array = new string [2, 2]; |
| 94 | |
| 95 | Assert.AreEqual (array, identity (array)); |
| 96 | } |
| 97 | |
| 98 | [Test] |
| 99 | public void ImportFieldStringEmpty () |
| 100 | { |
| 101 | var get_empty = Compile<Func<string>> ((module, body) => { |
| 102 | var il = body.GetILProcessor (); |
| 103 | il.Emit (OpCodes.Ldsfld, module.Import (typeof (string).GetField ("Empty"))); |
| 104 | il.Emit (OpCodes.Ret); |
| 105 | }); |
| 106 | |
| 107 | Assert.AreEqual ("", get_empty ()); |
| 108 | } |
| 109 | |
| 110 | [Test] |
| 111 | public void ImportStringConcat () |
| 112 | { |
| 113 | var concat = Compile<Func<string, string, string>> ((module, body) => { |
| 114 | var il = body.GetILProcessor (); |
| 115 | il.Emit (OpCodes.Ldarg_0); |
| 116 | il.Emit (OpCodes.Ldarg_1); |
| 117 | il.Emit (OpCodes.Call, module.Import (typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }))); |
| 118 | il.Emit (OpCodes.Ret); |
| 119 | }); |
| 120 | |
| 121 | Assert.AreEqual ("FooBar", concat ("Foo", "Bar")); |
| 122 | } |
| 123 | |
| 124 | public class Generic<T> { |
| 125 | public T Field; |
| 126 | |
| 127 | public T Method (T t) |
| 128 | { |
| 129 | return t; |
| 130 | } |
| 131 | |
| 132 | public TS GenericMethod<TS> (T t, TS s) |
| 133 | { |
| 134 | return s; |
| 135 | } |
| 136 | |
| 137 | public Generic<TS> ComplexGenericMethod<TS> (T t, TS s) |
| 138 | { |
| 139 | return new Generic<TS> { Field = s }; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | [Test] |
| 144 | public void ImportGenericField () |
| 145 | { |
| 146 | var get_field = Compile<Func<Generic<string>, string>> ((module, body) => { |
| 147 | var il = body.GetILProcessor (); |
| 148 | il.Emit (OpCodes.Ldarg_0); |
| 149 | il.Emit (OpCodes.Ldfld, module.Import (typeof (Generic<string>).GetField ("Field"))); |
| 150 | il.Emit (OpCodes.Ret); |
| 151 | }); |
| 152 | |
| 153 | var generic = new Generic<string> { |
| 154 | Field = "foo", |
| 155 | }; |
| 156 | |
| 157 | Assert.AreEqual ("foo", get_field (generic)); |
| 158 | } |
| 159 | |
| 160 | [Test] |
| 161 | public void ImportGenericMethod () |
| 162 | { |
| 163 | var generic_identity = Compile<Func<Generic<int>, int, int>> ((module, body) => { |
| 164 | var il = body.GetILProcessor (); |
| 165 | il.Emit (OpCodes.Ldarg_0); |
| 166 | il.Emit (OpCodes.Ldarg_1); |
| 167 | il.Emit (OpCodes.Callvirt, module.Import (typeof (Generic<int>).GetMethod ("Method"))); |
| 168 | il.Emit (OpCodes.Ret); |
| 169 | }); |
| 170 | |
| 171 | Assert.AreEqual (42, generic_identity (new Generic<int> (), 42)); |
| 172 | } |
| 173 | |
| 174 | [Test] |
| 175 | public void ImportGenericMethodSpec () |
| 176 | { |
| 177 | var gen_spec_id = Compile<Func<Generic<string>, int, int>> ((module, body) => { |
| 178 | var il = body.GetILProcessor (); |
| 179 | il.Emit (OpCodes.Ldarg_0); |
| 180 | il.Emit (OpCodes.Ldnull); |
| 181 | il.Emit (OpCodes.Ldarg_1); |
| 182 | il.Emit (OpCodes.Callvirt, module.Import (typeof (Generic<string>).GetMethod ("GenericMethod").MakeGenericMethod (typeof (int)))); |
| 183 | il.Emit (OpCodes.Ret); |
| 184 | }); |
| 185 | |
| 186 | Assert.AreEqual (42, gen_spec_id (new Generic<string> (), 42)); |
| 187 | } |
| 188 | |
| 189 | [Test] |
| 190 | public void ImportComplexGenericMethodSpec () |
| 191 | { |
| 192 | var gen_spec_id = Compile<Func<Generic<string>, int, int>> ((module, body) => { |
| 193 | var il = body.GetILProcessor (); |
| 194 | il.Emit (OpCodes.Ldarg_0); |
| 195 | il.Emit (OpCodes.Ldnull); |
| 196 | il.Emit (OpCodes.Ldarg_1); |
| 197 | il.Emit (OpCodes.Callvirt, module.Import (typeof (Generic<string>).GetMethod ("ComplexGenericMethod").MakeGenericMethod (typeof (int)))); |
| 198 | il.Emit (OpCodes.Ldfld, module.Import (typeof (Generic<string>).GetField ("Field"))); |
| 199 | il.Emit (OpCodes.Ret); |
| 200 | }); |
| 201 | |
| 202 | Assert.AreEqual (42, gen_spec_id (new Generic<string> (), 42)); |
| 203 | } |
| 204 | |
| 205 | public class Foo<TFoo> { |
| 206 | public List<TFoo> list; |
| 207 | } |
| 208 | |
| 209 | [Test] |
| 210 | public void ImportGenericTypeDefOrOpen () |
| 211 | { |
| 212 | var module = typeof (Foo<>).ToDefinition ().Module; |
| 213 | |
| 214 | var foo_def = module.Import (typeof (Foo<>)); |
| 215 | var foo_open = module.Import (typeof (Foo<>), foo_def); |
| 216 | |
| 217 | Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1", foo_def.FullName); |
| 218 | Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>", foo_open.FullName); |
| 219 | } |
| 220 | |
| 221 | [Test] |
| 222 | public void ImportGenericTypeFromContext () |
| 223 | { |
| 224 | var list_foo = typeof (Foo<>).GetField ("list").FieldType; |
| 225 | var generic_list_foo_open = typeof (Generic<>).MakeGenericType (list_foo); |
| 226 | |
| 227 | var foo_def = typeof (Foo<>).ToDefinition (); |
| 228 | var module = foo_def.Module; |
| 229 | |
| 230 | var generic_foo = module.Import (generic_list_foo_open, foo_def); |
| 231 | |
| 232 | Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>", |
| 233 | generic_foo.FullName); |
| 234 | } |
| 235 | |
| 236 | [Test] |
| 237 | public void ImportGenericTypeDefFromContext () |
| 238 | { |
| 239 | var foo_open = typeof (Foo<>).MakeGenericType (typeof (Foo<>).GetGenericArguments () [0]); |
| 240 | var generic_foo_open = typeof (Generic<>).MakeGenericType (foo_open); |
| 241 | |
| 242 | var foo_def = typeof (Foo<>).ToDefinition (); |
| 243 | var module = foo_def.Module; |
| 244 | |
| 245 | var generic_foo = module.Import (generic_foo_open, foo_def); |
| 246 | |
| 247 | Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>>", |
| 248 | generic_foo.FullName); |
| 249 | } |
| 250 | |
| 251 | [Test] |
| 252 | public void ImportArrayTypeDefFromContext () |
| 253 | { |
| 254 | var foo_open = typeof (Foo<>).MakeGenericType (typeof (Foo<>).GetGenericArguments () [0]); |
| 255 | var foo_open_array = foo_open.MakeArrayType (); |
| 256 | |
| 257 | var foo_def = typeof (Foo<>).ToDefinition (); |
| 258 | var module = foo_def.Module; |
| 259 | |
| 260 | var array_foo = module.Import (foo_open_array, foo_def); |
| 261 | |
| 262 | Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>[]", |
| 263 | array_foo.FullName); |
| 264 | } |
| 265 | |
| 266 | [Test] |
| 267 | public void ImportGenericFieldFromContext () |
| 268 | { |
| 269 | var list_foo = typeof (Foo<>).GetField ("list").FieldType; |
| 270 | var generic_list_foo_open = typeof (Generic<>).MakeGenericType (list_foo); |
| 271 | var generic_list_foo_open_field = generic_list_foo_open.GetField ("Field"); |
| 272 | |
| 273 | var foo_def = typeof (Foo<>).ToDefinition (); |
| 274 | var module = foo_def.Module; |
| 275 | |
| 276 | var generic_field = module.Import (generic_list_foo_open_field, foo_def); |
| 277 | |
| 278 | Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Field", |
| 279 | generic_field.FullName); |
| 280 | } |
| 281 | |
| 282 | [Test] |
| 283 | public void ImportGenericMethodFromContext () |
| 284 | { |
| 285 | var list_foo = typeof (Foo<>).GetField ("list").FieldType; |
| 286 | var generic_list_foo_open = typeof (Generic<>).MakeGenericType (list_foo); |
| 287 | var generic_list_foo_open_method = generic_list_foo_open.GetMethod ("Method"); |
| 288 | |
| 289 | var foo_def = typeof (Foo<>).ToDefinition (); |
| 290 | var module = foo_def.Module; |
| 291 | |
| 292 | var generic_method = module.Import (generic_list_foo_open_method, foo_def); |
| 293 | |
| 294 | Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Method(T)", |
| 295 | generic_method.FullName); |
| 296 | } |
| 297 | |
| 298 | [Test] |
| 299 | public void ImportMethodOnOpenGenericType () |
| 300 | { |
| 301 | var module = typeof (Generic<>).ToDefinition ().Module; |
| 302 | |
| 303 | var method = module.Import (typeof (Generic<>).GetMethod ("Method")); |
| 304 | |
| 305 | Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::Method(T)", method.FullName); |
| 306 | } |
| 307 | |
| 308 | [Test] |
| 309 | public void ImportGenericMethodOnOpenGenericType () |
| 310 | { |
| 311 | var module = typeof (Generic<>).ToDefinition ().Module; |
| 312 | |
| 313 | var generic_method = module.Import (typeof (Generic<>).GetMethod ("GenericMethod")); |
| 314 | |
| 315 | Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod(T,TS)", generic_method.FullName); |
| 316 | |
| 317 | generic_method = module.Import (typeof (Generic<>).GetMethod ("GenericMethod"), generic_method); |
| 318 | |
| 319 | Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod<TS>(T,TS)", generic_method.FullName); |
| 320 | } |
| 321 | |
| 322 | delegate void Emitter (ModuleDefinition module, MethodBody body); |
| 323 | |
| 324 | [MethodImpl (MethodImplOptions.NoInlining)] |
| 325 | static TDelegate Compile<TDelegate> (Emitter emitter) |
| 326 | where TDelegate : class |
| 327 | { |
| 328 | var name = GetTestCaseName (); |
| 329 | |
| 330 | var module = CreateTestModule<TDelegate> (name, emitter); |
| 331 | var assembly = LoadTestModule (module); |
| 332 | |
| 333 | return CreateRunDelegate<TDelegate> (GetTestCase (name, assembly)); |
| 334 | } |
| 335 | |
| 336 | static TDelegate CreateRunDelegate<TDelegate> (Type type) |
| 337 | where TDelegate : class |
| 338 | { |
| 339 | return (TDelegate) (object) Delegate.CreateDelegate (typeof (TDelegate), type.GetMethod ("Run")); |
| 340 | } |
| 341 | |
| 342 | static Type GetTestCase (string name, SR.Assembly assembly) |
| 343 | { |
| 344 | return assembly.GetType (name); |
| 345 | } |
| 346 | |
| 347 | static SR.Assembly LoadTestModule (ModuleDefinition module) |
| 348 | { |
| 349 | using (var stream = new MemoryStream ()) { |
| 350 | module.Write (stream); |
| 351 | File.WriteAllBytes (Path.Combine (Path.Combine (Path.GetTempPath (), "cecil"), module.Name + ".dll"), stream.ToArray ()); |
| 352 | return SR.Assembly.Load (stream.ToArray ()); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | static ModuleDefinition CreateTestModule<TDelegate> (string name, Emitter emitter) |
| 357 | { |
| 358 | var module = CreateModule (name); |
| 359 | |
| 360 | var type = new TypeDefinition ( |
| 361 | "", |
| 362 | name, |
| 363 | TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Abstract, |
| 364 | module.Import (typeof (object))); |
| 365 | |
| 366 | module.Types.Add (type); |
| 367 | |
| 368 | var method = CreateMethod (type, typeof (TDelegate).GetMethod ("Invoke")); |
| 369 | |
| 370 | emitter (module, method.Body); |
| 371 | |
| 372 | return module; |
| 373 | } |
| 374 | |
| 375 | static MethodDefinition CreateMethod (TypeDefinition type, SR.MethodInfo pattern) |
| 376 | { |
| 377 | var module = type.Module; |
| 378 | |
| 379 | var method = new MethodDefinition { |
| 380 | Name = "Run", |
| 381 | IsPublic = true, |
| 382 | IsStatic = true, |
| 383 | }; |
| 384 | |
| 385 | type.Methods.Add (method); |
| 386 | |
| 387 | method.MethodReturnType.ReturnType = module.Import (pattern.ReturnType); |
| 388 | |
| 389 | foreach (var parameter_pattern in pattern.GetParameters ()) |
| 390 | method.Parameters.Add (new ParameterDefinition (module.Import (parameter_pattern.ParameterType))); |
| 391 | |
| 392 | return method; |
| 393 | } |
| 394 | |
| 395 | static ModuleDefinition CreateModule (string name) |
| 396 | { |
| 397 | return ModuleDefinition.CreateModule (name, ModuleKind.Dll); |
| 398 | } |
| 399 | |
| 400 | [MethodImpl (MethodImplOptions.NoInlining)] |
| 401 | static string GetTestCaseName () |
| 402 | { |
| 403 | var stack_trace = new StackTrace (); |
| 404 | var stack_frame = stack_trace.GetFrame (2); |
| 405 | |
| 406 | return "ImportReflection_" + stack_frame.GetMethod ().Name; |
| 407 | } |
| 408 | } |
| 409 | } |