X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FTest%2FSystem.Reflection%2FFieldInfoTest.cs;h=d6e4c954d92070a0afd1f124df1b4cfa5b786ceb;hb=ecd61be7b3ef7aa871d3bd7be9669d69dd6812b6;hp=f015b7f27d40df3e9e8df05dd272f7364a119501;hpb=476a3e2d57028bb97f26dd53f7ce6f9c3756407d;p=mono.git diff --git a/mcs/class/corlib/Test/System.Reflection/FieldInfoTest.cs b/mcs/class/corlib/Test/System.Reflection/FieldInfoTest.cs index f015b7f27d4..d6e4c954d92 100644 --- a/mcs/class/corlib/Test/System.Reflection/FieldInfoTest.cs +++ b/mcs/class/corlib/Test/System.Reflection/FieldInfoTest.cs @@ -101,7 +101,7 @@ public class FieldInfoTest : Assertion public void GetValueOnRefOnlyAssembly () { Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName); - Type t = assembly.GetType (typeof (RefOnlyClass).FullName); + Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName); FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic); f.GetValue (null); @@ -112,20 +112,77 @@ public class FieldInfoTest : Assertion public void SetValueOnRefOnlyAssembly () { Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName); - Type t = assembly.GetType (typeof (RefOnlyClass).FullName); + Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName); FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic); 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; + + [Test] + public void NullableTests () + { + FieldInfoTest t = new FieldInfoTest (); + + FieldInfo fi = typeof (FieldInfoTest).GetField ("nullable_field"); + + fi.SetValue (t, 101); + AssertEquals (101, fi.GetValue (t)); + fi.SetValue (t, null); + AssertEquals (null, fi.GetValue (t)); + + FieldInfo fi2 = typeof (FieldInfoTest).GetField ("static_nullable_field"); + + fi2.SetValue (t, 101); + AssertEquals (101, fi2.GetValue (t)); + 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 -class RefOnlyClass +// Helper classes +class RefOnlyFieldClass { // Helper property static int RefOnlyField; } + +class NonPublicFieldClass +{ + protected int protectedField; +} #endif }