Grasshopper project system now uses csproj extension
[mono.git] / mcs / class / corlib / Test / System.Reflection / FieldInfoTest.cs
index e9319385f200a3d5e1cdd46e2b44df6c689c0229..d6e4c954d92070a0afd1f124df1b4cfa5b786ceb 100644 (file)
@@ -118,6 +118,16 @@ public class FieldInfoTest : Assertion
                f.SetValue (null, 8);
        }
 
+       const int literal = 42;
+
+       [Test]
+       [ExpectedException (typeof (FieldAccessException))]
+       public void SetValueOnLiteralField ()
+       {
+               FieldInfo f = typeof (FieldInfoTest).GetField ("literal", BindingFlags.Static | BindingFlags.NonPublic);
+               f.SetValue (null, 0);
+       }
+
        public int? nullable_field;
 
        public static int? static_nullable_field;
@@ -141,14 +151,38 @@ public class FieldInfoTest : Assertion
                fi2.SetValue (t, null);
                AssertEquals (null, fi2.GetValue (t));
        }
+       
+       [Test]
+       public void NonPublicTests ()
+       {               
+               Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
+               
+               Type t = assembly.GetType (typeof (NonPublicFieldClass).FullName);
+
+               // try to get non-public field
+               FieldInfo fi = t.GetField ("protectedField");
+               AssertNull (fi);
+               // get it for real
+               fi = t.GetField ("protectedField", BindingFlags.NonPublic | BindingFlags.Instance);
+               AssertNotNull (fi);             
+               // get via typebuilder          
+               FieldInfo f = TypeBuilder.GetField (t, fi);
+               AssertNotNull (f);      
+       }
+       
 #endif
 }              
 #if NET_2_0
-// Helper class
+// Helper classes
 class RefOnlyFieldClass 
 {
        // Helper property
        static int RefOnlyField;
 }
+
+class NonPublicFieldClass
+{
+       protected int protectedField;
+}
 #endif
 }