From: Rodrigo Kumpera Date: Tue, 1 Feb 2011 17:08:36 +0000 (+0100) Subject: Add test for dynamic method resurrection. X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=b9d5892425cfbbeb561935284f202708e16f0130;p=mono.git Add test for dynamic method resurrection. --- diff --git a/mono/tests/Makefile.am b/mono/tests/Makefile.am index 0e9c429ee2a..a7145476017 100644 --- a/mono/tests/Makefile.am +++ b/mono/tests/Makefile.am @@ -371,7 +371,8 @@ BASE_TEST_CS_SRC= \ bug-575941.cs \ bug-599469.cs \ bug-389886-3.cs \ - monitor.cs + monitor.cs \ + dynamic-method-resurrection.cs TEST_CS_SRC_DIST= \ $(BASE_TEST_CS_SRC) \ diff --git a/mono/tests/dynamic-method-resurrection.cs b/mono/tests/dynamic-method-resurrection.cs new file mode 100644 index 00000000000..bd7bc5f315b --- /dev/null +++ b/mono/tests/dynamic-method-resurrection.cs @@ -0,0 +1,63 @@ +using System; +using System.Reflection; +using System.Reflection.Emit; +using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; + +delegate int Getter (); + +class Host { + + static int Field = 42; + + Getter g; + + public Host (Getter g) { + this.g = g; + } + + ~Host () { + Program.resed = g; + } +} + + +class Program { + internal static Getter resed; + static int result; + static void DoStuff () + { + DynamicMethod method = new DynamicMethod ("GetField", + typeof (int), new Type [0], Type.GetType ("Host")); + + ILGenerator il = method.GetILGenerator (); + il.Emit (OpCodes.Ldsfld, typeof (Host).GetField ("Field", BindingFlags.Static | BindingFlags.NonPublic)); + il.Emit (OpCodes.Ret); + + var g = (Getter)method.CreateDelegate (typeof (Getter)); + new Host (g); + } + + static bool CheckStuff () { + if (resed == null) + return false; + Program.result = resed (); + resed = null; + return true; + } + + public static int Main () + { + int cnt = 5; + DoStuff (); + do { + if (CheckStuff ()) + break; + GC.Collect (); + GC.WaitForPendingFinalizers (); + } while (cnt-- > 0); + GC.Collect (); + GC.WaitForPendingFinalizers (); + return result == 42 ? 0 : 1; + } +}