[Mono-bugs] [Bug 80487][Wis] Changed - Module static constructor of libraries is not executed

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Fri Jan 12 12:26:24 EST 2007


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by akyrtzi at gmail.com.

http://bugzilla.ximian.com/show_bug.cgi?id=80487

--- shadow/80487	2007-01-12 11:15:38.000000000 -0500
+++ shadow/80487.tmp.22763	2007-01-12 12:26:24.000000000 -0500
@@ -94,6 +94,105 @@
 I did a few tests and the module constructor code is run as soon as
 the assembly is loaded.
 
 ------- Additional Comments From lupus at ximian.com  2007-01-12 11:15 -------
 Can you show the test: did you just load the assembly without
 accessing methods or fields?
+
+------- Additional Comments From akyrtzi at gmail.com  2007-01-12 12:26 -------
+Ok, here's a better test that shows that I was wrong, the module
+constructor is executed just before a field/method is used.
+
+First the IL sample:
+
+.assembly TestDll { }
+.assembly extern mscorlib { }
+
+.method assembly specialname rtspecialname static 
+        void  .cctor() cil managed
+{
+	ldstr "Module contructor executed"
+	call void [mscorlib]System.Console::WriteLine(string)
+	ret
+}
+
+.namespace NS
+{
+	.class public TestClass extends [mscorlib]System.Object
+	{
+		.field public static int32 TestField
+	
+		.method public static void  TestMethod() cil managed
+		{
+		  ldstr "TestMethod executed"
+		  call void [mscorlib]System.Console::WriteLine(string)
+		  ret
+		}
+	}
+}
+
+
+And the Test.cs:
+
+
+using System;
+
+public class Program
+{
+    public static void Main()
+    {
+        System.Reflection.Assembly asm =
+System.Reflection.Assembly.LoadFrom("TestDll.dll");
+        Console.WriteLine("assembly loaded");
+        Type type = asm.GetType("NS.TestClass", true);
+        Console.WriteLine("got type 'NS.TestClass'");
+
+        System.Reflection.MethodInfo method =
+type.GetMethod("TestMethod");
+        Console.WriteLine("got method 'TestMethod'");
+        Console.WriteLine("about to invoke 'TestMethod'");
+        method.Invoke(null, null);
+    }
+}
+
+
+this will show:
+
+assembly loaded
+got type 'NS.TestClass'
+got method 'TestMethod'
+about to invoke 'TestMethod'
+Module contructor executed
+TestMethod executed
+
+
+For Test2.cs:
+
+
+using System;
+
+public class Program
+{
+    public static void Main()
+    {
+        System.Reflection.Assembly asm =
+System.Reflection.Assembly.LoadFrom("TestDll.dll");
+        Console.WriteLine("assembly loaded");
+        Type type = asm.GetType("NS.TestClass", true);
+        Console.WriteLine("got type 'NS.TestClass'");
+
+        System.Reflection.FieldInfo field = type.GetField("TestField");
+        Console.WriteLine("about to get value of 'TestField'");
+        Console.WriteLine("got field 'TestField'");
+        Console.WriteLine("Value of field: " + field.GetValue(null));
+    }
+}
+
+
+will show:
+
+assembly loaded
+got type 'NS.TestClass'
+got field 'TestField'
+about to get value of 'TestField'
+Module contructor executed
+Value of field: 0


More information about the mono-bugs mailing list