[Mono-bugs] [Bug 75515][Wis] New - Accessing inherited static fields via reflection with the BindingFlags.FlattenHierarchy flag yields wrong value

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Sat Jul 9 10:39:39 EDT 2005


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 rodrigobamboo at gmail.com.

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

--- shadow/75515	2005-07-09 10:39:39.000000000 -0400
+++ shadow/75515.tmp.31846	2005-07-09 10:39:39.000000000 -0400
@@ -0,0 +1,78 @@
+Bug#: 75515
+Product: Mono: Class Libraries
+Version: 1.0
+OS: 
+OS Details: ubuntu, mono revision 47133 --with-preview=no
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Wishlist
+Component: CORLIB
+AssignedTo: mono-bugs at ximian.com                            
+ReportedBy: rodrigobamboo at gmail.com               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Accessing inherited static fields via reflection with the BindingFlags.FlattenHierarchy flag yields wrong value
+
+using System;
+using System.Reflection;
+
+class X
+{
+    public static int Value;
+}
+
+class Y  : X
+{
+}
+
+public class Program
+{
+    const BindingFlags DefaultBindingFlags = BindingFlags.Public |
+                                                BindingFlags.Static |
+                                                BindingFlags.FlattenHierarchy;
+
+    const BindingFlags SetBindingFlags = DefaultBindingFlags |
+                                                BindingFlags.SetField;
+
+    const BindingFlags GetBindingFlags = DefaultBindingFlags |
+                                                BindingFlags.GetField;
+
+    static void Set(Type type, string name, object value)
+    {
+        type.InvokeMember(name,
+            SetBindingFlags,
+            null,
+            null,
+            new object[] { value });
+    }
+
+    static object Get(Type type, string name)
+    {
+        return type.InvokeMember(name,
+            GetBindingFlags,
+            null,
+            null,
+            new object[0]);
+    }
+
+    static void Main(string[] args)
+    {
+        Set(typeof(X), "Value", 5);
+        assert(5 == X.Value);
+        assert(5 == (int)Get(typeof(X), "Value"));
+
+        assert(5 == Y.Value);
+        assert(5 == (int)Get(typeof(Y), "Value"));
+    }
+
+    static void assert(bool condition)
+    {
+        if (!condition)
+        {
+            throw new ApplicationException();
+        }
+    }
+}


More information about the mono-bugs mailing list