2 // FieldInfoTest - NUnit Test Cases for the FieldInfo class
4 // Zoltan Varga (vargaz@freemail.hu)
6 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Threading;
31 using System.Reflection;
33 using System.Reflection.Emit;
35 using System.Runtime.InteropServices;
37 using NUnit.Framework;
39 namespace MonoTests.System.Reflection
41 [StructLayout(LayoutKind.Explicit, Pack = 4, Size = 64)]
48 [StructLayout(LayoutKind.Sequential)]
51 [MarshalAsAttribute(UnmanagedType.Bool)]
54 [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]
57 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
60 // This doesn't work under mono
61 //[MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")]
66 public class FieldInfoTest
72 public void IsDefined_AttributeType_Null ()
74 Type type = typeof (FieldInfoTest);
75 FieldInfo field = type.GetField ("i");
78 field.IsDefined ((Type) null, false);
80 } catch (ArgumentNullException ex) {
81 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
82 Assert.IsNull (ex.InnerException, "#3");
83 Assert.IsNotNull (ex.Message, "#4");
84 Assert.IsNotNull (ex.ParamName, "#5");
85 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
91 public void PseudoCustomAttributes ()
93 Type t = typeof (FieldInfoTest);
95 Assert.AreEqual (1, t.GetField ("i").GetCustomAttributes (typeof (NonSerializedAttribute), true).Length);
97 FieldOffsetAttribute field_attr = (FieldOffsetAttribute)(typeof (Class1).GetField ("i").GetCustomAttributes (true) [0]);
98 Assert.AreEqual (32, field_attr.Value);
100 MarshalAsAttribute attr;
102 attr = (MarshalAsAttribute)typeof (Class2).GetField ("f0").GetCustomAttributes (true) [0];
103 Assert.AreEqual (UnmanagedType.Bool, attr.Value);
105 attr = (MarshalAsAttribute)typeof (Class2).GetField ("f1").GetCustomAttributes (true) [0];
106 Assert.AreEqual (UnmanagedType.LPArray, attr.Value);
107 Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType);
109 attr = (MarshalAsAttribute)typeof (Class2).GetField ("f2").GetCustomAttributes (true) [0];
110 Assert.AreEqual (UnmanagedType.ByValTStr, attr.Value);
111 Assert.AreEqual (100, attr.SizeConst);
114 attr = (MarshalAsAttribute)typeof (Class2).GetField ("f3").GetCustomAttributes (true) [0];
115 Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value);
116 Assert.AreEqual ("5", attr.MarshalCookie);
117 Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType));
121 #if !TARGET_JVM // ReflectionOnlyLoad not supported for TARGET_JVM
123 [ExpectedException (typeof (InvalidOperationException))]
124 public void GetValueOnRefOnlyAssembly ()
126 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
127 Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName);
128 FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic);
133 [ExpectedException (typeof (InvalidOperationException))]
134 public void SetValueOnRefOnlyAssembly ()
136 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
137 Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName);
138 FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic);
139 f.SetValue (null, 8);
143 const int literal = 42;
146 [ExpectedException (typeof (FieldAccessException))]
147 public void SetValueOnLiteralField ()
149 FieldInfo f = typeof (FieldInfoTest).GetField ("literal", BindingFlags.Static | BindingFlags.NonPublic);
150 f.SetValue (null, 0);
153 public int? nullable_field;
155 public static int? static_nullable_field;
158 public void NullableTests ()
160 FieldInfoTest t = new FieldInfoTest ();
162 FieldInfo fi = typeof (FieldInfoTest).GetField ("nullable_field");
164 fi.SetValue (t, 101);
165 Assert.AreEqual (101, fi.GetValue (t));
166 fi.SetValue (t, null);
167 Assert.AreEqual (null, fi.GetValue (t));
169 FieldInfo fi2 = typeof (FieldInfoTest).GetField ("static_nullable_field");
171 fi2.SetValue (t, 101);
172 Assert.AreEqual (101, fi2.GetValue (t));
173 fi2.SetValue (t, null);
174 Assert.AreEqual (null, fi2.GetValue (t));
177 #if !TARGET_JVM // TypeBuilder not supported for TARGET_JVM
179 public void NonPublicTests ()
181 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
183 Type t = assembly.GetType (typeof (NonPublicFieldClass).FullName);
185 // try to get non-public field
186 FieldInfo fi = t.GetField ("protectedField");
189 fi = t.GetField ("protectedField", BindingFlags.NonPublic | BindingFlags.Instance);
190 Assert.IsNotNull (fi);
191 // get via typebuilder
192 FieldInfo f = TypeBuilder.GetField (t, fi);
193 Assert.IsNotNull (f);
202 class RefOnlyFieldClass
205 static int RefOnlyField;
208 class NonPublicFieldClass
210 protected int protectedField;