New test.
[mono.git] / mcs / class / corlib / Test / System.Reflection / FieldInfoTest.cs
index c421a227f1b7527e704c24aebb1cee81448efa05..4ad97602f8f7b3d9a75fbdf52ba637501d70e3c7 100644 (file)
@@ -43,6 +43,22 @@ public class Class1 {
        public int i;
 }
 
+[StructLayout(LayoutKind.Sequential)]
+public class Class2 {
+       [MarshalAsAttribute(UnmanagedType.Bool)]
+       public int f0;
+
+       [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]
+       public string[] f1;
+
+       [MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
+       public string f2;
+
+       // This doesn't work under mono
+       //[MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")]
+       //public object f3;
+}
+
 [TestFixture]
 public class FieldInfoTest : Assertion
 {
@@ -56,9 +72,93 @@ public class FieldInfoTest : Assertion
 
                AssertEquals (1, t.GetField ("i").GetCustomAttributes (typeof (NonSerializedAttribute), true).Length);
 
-               FieldOffsetAttribute attr = (FieldOffsetAttribute)(typeof (Class1).GetField ("i").GetCustomAttributes (true) [0]);
-               AssertEquals (32, attr.Value);
+               FieldOffsetAttribute field_attr = (FieldOffsetAttribute)(typeof (Class1).GetField ("i").GetCustomAttributes (true) [0]);
+               AssertEquals (32, field_attr.Value);
+
+               MarshalAsAttribute attr;
+
+               attr = (MarshalAsAttribute)typeof (Class2).GetField ("f0").GetCustomAttributes (true) [0];
+               AssertEquals (UnmanagedType.Bool, attr.Value);
+
+               attr = (MarshalAsAttribute)typeof (Class2).GetField ("f1").GetCustomAttributes (true) [0];
+               AssertEquals (UnmanagedType.LPArray, attr.Value);
+               AssertEquals (UnmanagedType.LPStr, attr.ArraySubType);
+
+               attr = (MarshalAsAttribute)typeof (Class2).GetField ("f2").GetCustomAttributes (true) [0];
+               AssertEquals (UnmanagedType.ByValTStr, attr.Value);
+               AssertEquals (100, attr.SizeConst);
+
+               /*
+               attr = (MarshalAsAttribute)typeof (Class2).GetField ("f3").GetCustomAttributes (true) [0];
+               AssertEquals (UnmanagedType.CustomMarshaler, attr.Value);
+               AssertEquals ("5", attr.MarshalCookie);
+               AssertEquals (typeof (Marshal1), Type.GetType (attr.MarshalType));
+               */
+       }
+
+       [Test]
+       [ExpectedException (typeof (InvalidOperationException))]
+       public void GetValueOnRefOnlyAssembly ()
+       {
+               Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
+               Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName);
+               FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic);
+
+               f.GetValue (null);
+       }
+       
+       [Test]
+       [ExpectedException (typeof (InvalidOperationException))]
+       public void SetValueOnRefOnlyAssembly ()
+       {
+               Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.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));
        }
 #endif
 }              
+#if NET_2_0
+// Helper class
+class RefOnlyFieldClass 
+{
+       // Helper property
+       static int RefOnlyField;
+}
+#endif
 }