* MonoCustomAttrs.cs: In IsDefined, throw ArgumentNullException if
[mono.git] / mcs / class / corlib / Test / System.Reflection / FieldInfoTest.cs
1 //
2 // FieldInfoTest - NUnit Test Cases for the FieldInfo class
3 //
4 // Zoltan Varga (vargaz@freemail.hu)
5 //
6 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.Threading;
31 using System.Reflection;
32 #if !TARGET_JVM
33 using System.Reflection.Emit;
34 #endif // TARGET_JVM
35 using System.Runtime.InteropServices;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Reflection
40 {
41         [StructLayout(LayoutKind.Explicit, Pack = 4, Size = 64)]
42         public class Class1
43         {
44                 [FieldOffset (32)]
45                 public int i;
46         }
47
48         [StructLayout(LayoutKind.Sequential)]
49         public class Class2
50         {
51                 [MarshalAsAttribute(UnmanagedType.Bool)]
52                 public int f0;
53
54                 [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]
55                 public string[] f1;
56
57                 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
58                 public string f2;
59
60                 // This doesn't work under mono
61                 //[MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")]
62                 //public object f3;
63         }
64
65         [TestFixture]
66         public class FieldInfoTest
67         {
68                 [NonSerialized]
69                 public int i;
70
71                 [Test]
72                 public void IsDefined_AttributeType_Null ()
73                 {
74                         Type type = typeof (FieldInfoTest);
75                         FieldInfo field = type.GetField ("i");
76
77                         try {
78                                 field.IsDefined ((Type) null, false);
79                                 Assert.Fail ("#1");
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");
86                         }
87                 }
88
89 #if NET_2_0
90                 [Test]
91                 public void PseudoCustomAttributes ()
92                 {
93                         Type t = typeof (FieldInfoTest);
94
95                         Assert.AreEqual (1, t.GetField ("i").GetCustomAttributes (typeof (NonSerializedAttribute), true).Length);
96
97                         FieldOffsetAttribute field_attr = (FieldOffsetAttribute)(typeof (Class1).GetField ("i").GetCustomAttributes (true) [0]);
98                         Assert.AreEqual (32, field_attr.Value);
99
100                         MarshalAsAttribute attr;
101
102                         attr = (MarshalAsAttribute)typeof (Class2).GetField ("f0").GetCustomAttributes (true) [0];
103                         Assert.AreEqual (UnmanagedType.Bool, attr.Value);
104
105                         attr = (MarshalAsAttribute)typeof (Class2).GetField ("f1").GetCustomAttributes (true) [0];
106                         Assert.AreEqual (UnmanagedType.LPArray, attr.Value);
107                         Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType);
108
109                         attr = (MarshalAsAttribute)typeof (Class2).GetField ("f2").GetCustomAttributes (true) [0];
110                         Assert.AreEqual (UnmanagedType.ByValTStr, attr.Value);
111                         Assert.AreEqual (100, attr.SizeConst);
112
113                         /*
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));
118                         */
119                 }
120
121 #if !TARGET_JVM // ReflectionOnlyLoad not supported for TARGET_JVM
122                 [Test]
123                 [ExpectedException (typeof (InvalidOperationException))]
124                 public void GetValueOnRefOnlyAssembly ()
125                 {
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);
129                         f.GetValue (null);
130                 }
131         
132                 [Test]
133                 [ExpectedException (typeof (InvalidOperationException))]
134                 public void SetValueOnRefOnlyAssembly ()
135                 {
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);
140                 }
141 #endif // TARGET_JVM
142
143                 const int literal = 42;
144
145                 [Test]
146                 [ExpectedException (typeof (FieldAccessException))]
147                 public void SetValueOnLiteralField ()
148                 {
149                         FieldInfo f = typeof (FieldInfoTest).GetField ("literal", BindingFlags.Static | BindingFlags.NonPublic);
150                         f.SetValue (null, 0);
151                 }
152
153                 public int? nullable_field;
154
155                 public static int? static_nullable_field;
156
157                 [Test]
158                 public void NullableTests ()
159                 {
160                         FieldInfoTest t = new FieldInfoTest ();
161
162                         FieldInfo fi = typeof (FieldInfoTest).GetField ("nullable_field");
163
164                         fi.SetValue (t, 101);
165                         Assert.AreEqual (101, fi.GetValue (t));
166                         fi.SetValue (t, null);
167                         Assert.AreEqual (null, fi.GetValue (t));
168
169                         FieldInfo fi2 = typeof (FieldInfoTest).GetField ("static_nullable_field");
170
171                         fi2.SetValue (t, 101);
172                         Assert.AreEqual (101, fi2.GetValue (t));
173                         fi2.SetValue (t, null);
174                         Assert.AreEqual (null, fi2.GetValue (t));
175                 }
176         
177 #if !TARGET_JVM // TypeBuilder not supported for TARGET_JVM
178                 [Test]
179                 public void NonPublicTests ()
180                 {
181                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
182                 
183                         Type t = assembly.GetType (typeof (NonPublicFieldClass).FullName);
184
185                         // try to get non-public field
186                         FieldInfo fi = t.GetField ("protectedField");
187                         Assert.IsNull (fi);
188                         // get it for real
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);
194                 }
195 #endif // TARGET_JVM
196         
197 #endif
198         }
199
200 #if NET_2_0
201         // Helper classes
202         class RefOnlyFieldClass 
203         {
204                 // Helper property
205                 static int RefOnlyField;
206         }
207
208         class NonPublicFieldClass
209         {
210                 protected int protectedField;
211         }
212 #endif
213 }