2 // FieldInfoTest - NUnit Test Cases for the FieldInfo class
5 // Zoltan Varga (vargaz@freemail.hu)
6 // Gert Driesen (drieseng@users.sourceforge.net)
8 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Threading;
33 using System.Reflection;
35 using System.Reflection.Emit;
37 using System.Runtime.InteropServices;
39 using NUnit.Framework;
41 namespace MonoTests.System.Reflection
43 [StructLayout(LayoutKind.Explicit, Pack = 4, Size = 64)]
50 [StructLayout(LayoutKind.Sequential)]
53 [MarshalAsAttribute(UnmanagedType.Bool)]
56 [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]
59 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
61 #if FEATURE_COMINTEROP
62 [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof (Marshal1), MarshalCookie="5")]
65 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")]
72 public class Class3 : Class2
76 // Disable this warning, as the purpose of this struct is to poke at the internal via reflection
77 #pragma warning disable 649
78 class FieldInvokeMatrix
80 public Byte field_Byte;
81 public SByte field_SByte;
82 public Boolean field_Boolean;
83 public Char field_Char;
84 public Int16 field_Int16;
85 public UInt16 field_UInt16;
86 public Int32 field_Int32;
87 public UInt32 field_UInt32;
88 public Int64 field_Int64;
89 public UInt64 field_UInt64;
90 public Single field_Single;
91 public Double field_Double;
92 public IntPtr field_IntPtr;
93 public UIntPtr field_UIntPtr;
94 public Decimal field_Decimal;
95 public DateTime field_DateTime;
96 public String field_String;
98 public ByteEnum field_ByteEnum;
99 public SByteEnum field_SByteEnum;
100 public Int16Enum field_Int16Enum;
101 public UInt16Enum field_UInt16Enum;
102 public Int32Enum field_Int32Enum;
103 public UInt32Enum field_UInt32Enum;
104 public Int64Enum field_Int64Enum;
105 public UInt64Enum field_UInt64Enum;
107 #pragma warning restore 649
109 public enum ByteEnum : byte
111 MaxValue = Byte.MaxValue
114 public enum SByteEnum : sbyte
116 MaxValue = SByte.MaxValue
119 public enum Int16Enum : short
121 MaxValue = Int16.MaxValue
124 public enum UInt16Enum : ushort
126 MaxValue = UInt16.MaxValue
129 public enum Int32Enum : int
131 MaxValue = Int32.MaxValue
134 public enum UInt32Enum: uint
136 MaxValue= UInt32.MaxValue
139 public enum Int64Enum : long
141 MaxValue = Int64.MaxValue
144 public enum UInt64Enum: ulong
146 MaxValue = UInt64.MaxValue
150 public unsafe class FieldInfoTest
156 public void IsDefined_AttributeType_Null ()
158 Type type = typeof (FieldInfoTest);
159 FieldInfo field = type.GetField ("i");
162 field.IsDefined ((Type) null, false);
164 } catch (ArgumentNullException ex) {
165 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
166 Assert.IsNull (ex.InnerException, "#3");
167 Assert.IsNotNull (ex.Message, "#4");
168 Assert.IsNotNull (ex.ParamName, "#5");
169 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
174 public void GetCustomAttributes ()
179 fi = typeof (Class2).GetField ("f5");
181 attrs = fi.GetCustomAttributes (false);
182 Assert.AreEqual (1, attrs.Length, "#B1");
183 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#B2");
184 attrs = fi.GetCustomAttributes (true);
185 Assert.AreEqual (1, attrs.Length, "#B3");
186 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#B4");
187 attrs = fi.GetCustomAttributes (typeof (MarshalAsAttribute), false);
188 Assert.AreEqual (0, attrs.Length, "#B5");
189 attrs = fi.GetCustomAttributes (typeof (MarshalAsAttribute), true);
190 Assert.AreEqual (0, attrs.Length, "#B6");
191 attrs = fi.GetCustomAttributes (typeof (ObsoleteAttribute), false);
192 Assert.AreEqual (1, attrs.Length, "#B7");
193 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#B8");
194 attrs = fi.GetCustomAttributes (typeof (ObsoleteAttribute), true);
195 Assert.AreEqual (1, attrs.Length, "#B9");
196 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#B10");
198 fi = typeof (Class3).GetField ("f5");
200 attrs = fi.GetCustomAttributes (false);
201 Assert.AreEqual (1, attrs.Length, "#D1");
202 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#D2");
203 attrs = fi.GetCustomAttributes (true);
204 Assert.AreEqual (1, attrs.Length, "#D3");
205 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#D4");
206 attrs = fi.GetCustomAttributes (typeof (MarshalAsAttribute), false);
207 Assert.AreEqual (0, attrs.Length, "#D5");
208 attrs = fi.GetCustomAttributes (typeof (MarshalAsAttribute), true);
209 Assert.AreEqual (0, attrs.Length, "#D6");
210 attrs = fi.GetCustomAttributes (typeof (ObsoleteAttribute), false);
211 Assert.AreEqual (1, attrs.Length, "#D7");
212 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#D8");
213 attrs = fi.GetCustomAttributes (typeof (ObsoleteAttribute), true);
214 Assert.AreEqual (1, attrs.Length, "#D9");
215 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#D10");
218 [Test] // GetFieldFromHandle (RuntimeFieldHandle)
219 public void GetFieldFromHandle1_Handle_Zero ()
221 RuntimeFieldHandle fh = new RuntimeFieldHandle ();
224 FieldInfo.GetFieldFromHandle (fh);
226 } catch (ArgumentException ex) {
227 // Handle is not initialized
228 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
229 Assert.IsNull (ex.InnerException, "#3");
230 Assert.IsNotNull (ex.Message, "#4");
231 Assert.IsNull (ex.ParamName, "#5");
235 [Test] // GetFieldFromHandle (RuntimeFieldHandle, RuntimeTypeHandle)
236 public void GetFieldFromHandle2_DeclaringType_Zero ()
238 RuntimeTypeHandle th = new RuntimeTypeHandle ();
239 FieldInfo fi1 = typeof (Class2).GetField ("f5");
240 RuntimeFieldHandle fh = fi1.FieldHandle;
242 FieldInfo fi2 = FieldInfo.GetFieldFromHandle (fh, th);
243 Assert.IsNotNull (fi2, "#1");
244 Assert.AreSame (fi1.DeclaringType, fi2.DeclaringType, "#2");
245 Assert.AreEqual (fi1.FieldType, fi2.FieldType, "#3");
246 Assert.AreEqual (fi1.Name, fi2.Name, "#4");
249 [Test] // GetFieldFromHandle (RuntimeFieldHandle, RuntimeTypeHandle)
250 public void GetFieldFromHandle2_Handle_Generic ()
252 FieldInfoTest<string> instance = new FieldInfoTest<string> ();
253 Type t = instance.GetType ();
255 FieldInfo fi1 = t.GetField ("TestField");
256 RuntimeFieldHandle fh = fi1.FieldHandle;
257 RuntimeTypeHandle th = t.TypeHandle;
259 FieldInfo fi2 = FieldInfo.GetFieldFromHandle (fh, th);
260 Assert.IsNotNull (fi2, "#1");
261 Assert.AreSame (t, fi2.DeclaringType, "#2");
262 Assert.AreEqual (typeof (string), fi2.FieldType, "#3");
263 Assert.AreEqual ("TestField", fi2.Name, "#4");
266 [Test] // GetFieldFromHandle (RuntimeFieldHandle, RuntimeTypeHandle)
267 [Category ("NotWorking")]
268 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=343449
269 public void GetFieldFromHandle2_Handle_GenericDefinition ()
271 Type t1 = typeof (FieldInfoTest<>);
272 FieldInfo fi1 = t1.GetField ("TestField");
273 RuntimeFieldHandle fh = fi1.FieldHandle;
275 FieldInfoTest<string> instance = new FieldInfoTest<string> ();
276 Type t2 = instance.GetType ();
277 RuntimeTypeHandle th = t2.TypeHandle;
279 FieldInfo fi2 = FieldInfo.GetFieldFromHandle (fh, th);
280 Assert.IsNotNull (fi2, "#1");
281 Assert.AreSame (t2, fi2.DeclaringType, "#2");
282 Assert.AreEqual (typeof (string), fi2.FieldType, "#3");
283 Assert.AreEqual ("TestField", fi2.Name, "#4");
286 [Test] // GetFieldFromHandle (RuntimeFieldHandle, RuntimeTypeHandle)
287 public void GetFieldFromHandle2_Handle_Zero ()
289 object instance = new Class2 ();
290 RuntimeTypeHandle th = Type.GetTypeHandle (instance);
291 RuntimeFieldHandle fh = new RuntimeFieldHandle ();
294 FieldInfo.GetFieldFromHandle (fh, th);
296 } catch (ArgumentException ex) {
297 // Handle is not initialized
298 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
299 Assert.IsNull (ex.InnerException, "#3");
300 Assert.IsNotNull (ex.Message, "#4");
301 Assert.IsNull (ex.ParamName, "#5");
306 [ExpectedException (typeof (ArgumentException))]
307 public void GetFieldFromHandle2_Incompatible ()
309 RuntimeFieldHandle fh = typeof (FieldInfoTest<int>).GetField ("TestField").FieldHandle;
311 FieldInfoTest<string> instance = new FieldInfoTest<string> ();
312 Type t2 = instance.GetType ();
313 RuntimeTypeHandle th = t2.TypeHandle;
315 FieldInfo fi2 = FieldInfo.GetFieldFromHandle (fh, th);
319 public void PseudoCustomAttributes ()
322 Type t = typeof (FieldInfoTest);
324 Assert.AreEqual (1, t.GetField ("i").GetCustomAttributes (typeof (NonSerializedAttribute), true).Length);
326 attrs = typeof (Class1).GetField ("i").GetCustomAttributes (true);
327 Assert.AreEqual (1, attrs.Length, "#B1");
328 FieldOffsetAttribute field_attr = (FieldOffsetAttribute) attrs [0];
329 Assert.AreEqual (32, field_attr.Value, "#B2");
331 MarshalAsAttribute attr;
333 attrs = typeof (Class2).GetField ("f0").GetCustomAttributes (true);
334 Assert.AreEqual (1, attrs.Length, "#C1");
335 attr = (MarshalAsAttribute) attrs [0];
336 Assert.AreEqual (UnmanagedType.Bool, attr.Value, "#C2");
338 attrs = typeof (Class2).GetField ("f1").GetCustomAttributes (true);
339 Assert.AreEqual (1, attrs.Length, "#D1");
340 attr = (MarshalAsAttribute) attrs [0];
341 Assert.AreEqual (UnmanagedType.LPArray, attr.Value, "#D2");
342 Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType, "#D3");
344 attrs = typeof (Class2).GetField ("f2").GetCustomAttributes (true);
345 Assert.AreEqual (1, attrs.Length, "#E1");
346 attr = (MarshalAsAttribute) attrs [0];
347 Assert.AreEqual (UnmanagedType.ByValTStr, attr.Value, "#E2");
348 Assert.AreEqual (100, attr.SizeConst, "#E3");
350 attrs = typeof (Class2).GetField ("f3").GetCustomAttributes (true);
351 Assert.AreEqual (1, attrs.Length, "#F1");
352 attr = (MarshalAsAttribute) attrs [0];
353 #if FEATURE_COMINTEROP
354 Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#F2");
356 Assert.AreEqual ("5", attr.MarshalCookie, "#F3");
357 Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#F4");
359 attrs = typeof (Class3).GetField ("f3").GetCustomAttributes (false);
360 Assert.AreEqual (1, attrs.Length, "#G1");
361 attr = (MarshalAsAttribute) attrs [0];
362 #if FEATURE_COMINTEROP
363 Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#G2");
365 Assert.AreEqual ("5", attr.MarshalCookie, "#G3");
366 Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#G4");
368 attrs = typeof (Class3).GetField ("f3").GetCustomAttributes (true);
369 Assert.AreEqual (1, attrs.Length, "#H1");
370 attr = (MarshalAsAttribute) attrs [0];
371 #if FEATURE_COMINTEROP
372 Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#H2");
374 Assert.AreEqual ("5", attr.MarshalCookie, "#H3");
375 Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#H4");
378 attrs = typeof (Class2).GetField ("f3").GetCustomAttributes (true);
379 Assert.AreEqual (1, attrs.Length, "#I1");
380 attr = (MarshalAsAttribute) attrs [0];
381 #if FEATURE_COMINTEROP
382 Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#I2");
384 Assert.AreEqual ("5", attr.MarshalCookie, "#I3");
385 Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#I4");
388 // Disable "field not used warning", this is intended.
389 #pragma warning disable 649
391 public static int static_field;
394 #pragma warning restore 649
396 [ExpectedException (typeof (ArgumentException))]
397 public void GetValueWrongObject ()
401 typeof (Foo).GetField ("field").GetValue (typeof (int));
404 public void GetValueWrongObjectStatic ()
408 // This is allowed in MS.NET
409 typeof (Foo).GetField ("static_field").GetValue (typeof (int));
413 [ExpectedException (typeof (InvalidOperationException))]
414 public void GetValueOnRefOnlyAssembly ()
416 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
417 Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName);
418 FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic);
423 [ExpectedException (typeof (InvalidOperationException))]
424 public void SetValueOnRefOnlyAssembly ()
426 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
427 Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName);
428 FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic);
429 f.SetValue (null, 8);
432 const int literal = 42;
435 [ExpectedException (typeof (FieldAccessException))]
436 public void SetValueOnLiteralField ()
438 FieldInfo f = typeof (FieldInfoTest).GetField ("literal", BindingFlags.Static | BindingFlags.NonPublic);
439 f.SetValue (null, 0);
442 public int? nullable_field;
444 public static int? static_nullable_field;
447 public void NullableTests ()
449 FieldInfoTest t = new FieldInfoTest ();
451 FieldInfo fi = typeof (FieldInfoTest).GetField ("nullable_field");
453 fi.SetValue (t, 101);
454 Assert.AreEqual (101, fi.GetValue (t));
455 fi.SetValue (t, null);
456 Assert.AreEqual (null, fi.GetValue (t));
458 FieldInfo fi2 = typeof (FieldInfoTest).GetField ("static_nullable_field");
460 fi2.SetValue (t, 101);
461 Assert.AreEqual (101, fi2.GetValue (t));
462 fi2.SetValue (t, null);
463 Assert.AreEqual (null, fi2.GetValue (t));
467 public void NonPublicTests ()
469 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
471 Type t = assembly.GetType (typeof (NonPublicFieldClass).FullName);
473 // try to get non-public field
474 FieldInfo fi = t.GetField ("protectedField");
477 fi = t.GetField ("protectedField", BindingFlags.NonPublic | BindingFlags.Instance);
478 Assert.IsNotNull (fi);
482 public void GetRawDefaultValue ()
484 Assert.AreEqual (5, typeof (FieldInfoTest).GetField ("int_field").GetRawConstantValue ());
485 Assert.AreEqual (Int64.MaxValue, typeof (FieldInfoTest).GetField ("long_field").GetRawConstantValue ());
486 Assert.AreEqual (2, typeof (FieldInfoTest).GetField ("int_enum_field").GetRawConstantValue ());
487 Assert.AreEqual (typeof (int), typeof (FieldInfoTest).GetField ("int_enum_field").GetRawConstantValue ().GetType ());
488 Assert.AreEqual (2, typeof (FieldInfoTest).GetField ("long_enum_field").GetRawConstantValue ());
489 Assert.AreEqual (typeof (long), typeof (FieldInfoTest).GetField ("long_enum_field").GetRawConstantValue ().GetType ());
490 Assert.AreEqual ("Hello", typeof (FieldInfoTest).GetField ("string_field").GetRawConstantValue ());
491 Assert.AreEqual (null, typeof (FieldInfoTest).GetField ("object_field").GetRawConstantValue ());
495 [ExpectedException (typeof (InvalidOperationException))]
496 public void GetRawDefaultValueNoDefault ()
498 typeof (FieldInfoTest).GetField ("non_const_field").GetRawConstantValue ();
502 [ExpectedException (typeof (InvalidOperationException))]
503 public void GetValueOpenGeneric ()
505 typeof(Foo<>).GetField ("field").GetValue (null);
509 [ExpectedException (typeof (InvalidOperationException))]
510 public void SetValueOpenGeneric ()
512 typeof(Foo<>).GetField ("field").SetValue (null, 0);
516 public void GetValueOnConstantOfOpenGeneric ()
518 Assert.AreEqual (10, typeof(Foo<>).GetField ("constant").GetValue (null), "#1");
519 Assert.AreEqual ("waa", typeof(Foo<>).GetField ("sconstant").GetValue (null), "#2");
520 Assert.AreEqual (IntEnum.Third, typeof(Foo<>).GetField ("econstant").GetValue (null), "#3");
523 public static unsafe void* ip;
526 public unsafe void GetSetValuePointers ()
528 Pointer p0 = (Pointer)typeof (FieldInfoTest).GetField ("ip").GetValue (null);
529 int *p0i = (int*)Pointer.Unbox (p0);
530 Assert.AreEqual (IntPtr.Zero, new IntPtr (p0i));
534 typeof (FieldInfoTest).GetField ("ip").SetValue (null, (IntPtr)p);
535 Pointer p2 = (Pointer)typeof (FieldInfoTest).GetField ("ip").GetValue (null);
537 int *pi = (int*)Pointer.Unbox (p2);
538 Assert.AreEqual (5, *pi);
540 typeof (FieldInfoTest).GetField ("ip").SetValue (null, (UIntPtr)p);
541 p2 = (Pointer)typeof (FieldInfoTest).GetField ("ip").GetValue (null);
543 pi = (int*)Pointer.Unbox (p2);
544 Assert.AreEqual (5, *pi);
548 public void SetValuePrimitiveConversions ()
551 var instance = new FieldInvokeMatrix ();
552 var fh = typeof (FieldInvokeMatrix);
554 field = fh.GetField ("field_Byte");
555 field.SetValue (instance, Byte.MaxValue);
556 Assert.AreEqual (Byte.MaxValue, instance.field_Byte);
557 Throws (field, instance, SByte.MaxValue);
558 Throws (field, instance, true);
559 Throws (field, instance, Char.MaxValue);
560 Throws (field, instance, Int16.MaxValue);
561 Throws (field, instance, UInt16.MaxValue);
562 Throws (field, instance, Int32.MaxValue);
563 Throws (field, instance, UInt32.MaxValue);
564 Throws (field, instance, Int64.MaxValue);
565 Throws (field, instance, UInt64.MaxValue);
566 Throws (field, instance, Single.MaxValue);
567 Throws (field, instance, Double.MaxValue);
568 Throws (field, instance, IntPtr.Zero);
569 Throws (field, instance, UIntPtr.Zero);
570 Throws (field, instance, Decimal.MaxValue);
571 Throws (field, instance, DateTime.MaxValue);
572 field.SetValue (instance, ByteEnum.MaxValue);
573 Assert.AreEqual (Byte.MaxValue, instance.field_Byte);
574 Throws (field, instance, SByteEnum.MaxValue);
575 Throws (field, instance, Int16Enum.MaxValue);
576 Throws (field, instance, UInt16Enum.MaxValue);
577 Throws (field, instance, Int32Enum.MaxValue);
578 Throws (field, instance, UInt32Enum.MaxValue);
579 Throws (field, instance, Int64Enum.MaxValue);
580 Throws (field, instance, UInt64Enum.MaxValue);
581 field = fh.GetField ("field_SByte");
582 Throws (field, instance, Byte.MaxValue);
583 field.SetValue (instance, SByte.MaxValue);
584 Assert.AreEqual (SByte.MaxValue, instance.field_SByte);
585 Throws (field, instance, true);
586 Throws (field, instance, Char.MaxValue);
587 Throws (field, instance, Int16.MaxValue);
588 Throws (field, instance, UInt16.MaxValue);
589 Throws (field, instance, Int32.MaxValue);
590 Throws (field, instance, UInt32.MaxValue);
591 Throws (field, instance, Int64.MaxValue);
592 Throws (field, instance, UInt64.MaxValue);
593 Throws (field, instance, Single.MaxValue);
594 Throws (field, instance, Double.MaxValue);
595 Throws (field, instance, IntPtr.Zero);
596 Throws (field, instance, UIntPtr.Zero);
597 Throws (field, instance, Decimal.MaxValue);
598 Throws (field, instance, DateTime.MaxValue);
599 Throws (field, instance, ByteEnum.MaxValue);
600 field.SetValue (instance, SByteEnum.MaxValue);
601 Assert.AreEqual (SByte.MaxValue, instance.field_SByte);
602 Throws (field, instance, Int16Enum.MaxValue);
603 Throws (field, instance, UInt16Enum.MaxValue);
604 Throws (field, instance, Int32Enum.MaxValue);
605 Throws (field, instance, UInt32Enum.MaxValue);
606 Throws (field, instance, Int64Enum.MaxValue);
607 Throws (field, instance, UInt64Enum.MaxValue);
608 field = fh.GetField ("field_Boolean");
609 Throws (field, instance, Byte.MaxValue);
610 Throws (field, instance, SByte.MaxValue);
611 field.SetValue (instance, true);
612 Assert.AreEqual (true, instance.field_Boolean);
613 Throws (field, instance, Char.MaxValue);
614 Throws (field, instance, Int16.MaxValue);
615 Throws (field, instance, UInt16.MaxValue);
616 Throws (field, instance, Int32.MaxValue);
617 Throws (field, instance, UInt32.MaxValue);
618 Throws (field, instance, Int64.MaxValue);
619 Throws (field, instance, UInt64.MaxValue);
620 Throws (field, instance, Single.MaxValue);
621 Throws (field, instance, Double.MaxValue);
622 Throws (field, instance, IntPtr.Zero);
623 Throws (field, instance, UIntPtr.Zero);
624 Throws (field, instance, Decimal.MaxValue);
625 Throws (field, instance, DateTime.MaxValue);
626 Throws (field, instance, ByteEnum.MaxValue);
627 Throws (field, instance, SByteEnum.MaxValue);
628 Throws (field, instance, Int16Enum.MaxValue);
629 Throws (field, instance, UInt16Enum.MaxValue);
630 Throws (field, instance, Int32Enum.MaxValue);
631 Throws (field, instance, UInt32Enum.MaxValue);
632 Throws (field, instance, Int64Enum.MaxValue);
633 Throws (field, instance, UInt64Enum.MaxValue);
634 field = fh.GetField ("field_Char");
635 field.SetValue (instance, Byte.MaxValue);
636 Assert.AreEqual (Byte.MaxValue, instance.field_Char);
637 Throws (field, instance, SByte.MaxValue);
638 Throws (field, instance, true);
639 field.SetValue (instance, Char.MaxValue);
640 Assert.AreEqual (Char.MaxValue, instance.field_Char);
641 Throws (field, instance, Int16.MaxValue);
642 field.SetValue (instance, UInt16.MaxValue);
643 Assert.AreEqual (UInt16.MaxValue, instance.field_Char);
644 Throws (field, instance, Int32.MaxValue);
645 Throws (field, instance, UInt32.MaxValue);
646 Throws (field, instance, Int64.MaxValue);
647 Throws (field, instance, UInt64.MaxValue);
648 Throws (field, instance, Single.MaxValue);
649 Throws (field, instance, Double.MaxValue);
650 Throws (field, instance, IntPtr.Zero);
651 Throws (field, instance, UIntPtr.Zero);
652 Throws (field, instance, Decimal.MaxValue);
653 Throws (field, instance, DateTime.MaxValue);
654 field.SetValue (instance, ByteEnum.MaxValue);
655 Assert.AreEqual (Byte.MaxValue, instance.field_Char);
656 Throws (field, instance, SByteEnum.MaxValue);
657 Throws (field, instance, Int16Enum.MaxValue);
658 field.SetValue (instance, UInt16Enum.MaxValue);
659 Assert.AreEqual (UInt16.MaxValue, instance.field_Char);
660 Throws (field, instance, Int32Enum.MaxValue);
661 Throws (field, instance, UInt32Enum.MaxValue);
662 Throws (field, instance, Int64Enum.MaxValue);
663 Throws (field, instance, UInt64Enum.MaxValue);
664 field = fh.GetField ("field_Int16");
665 field.SetValue (instance, Byte.MaxValue);
666 Assert.AreEqual (Byte.MaxValue, instance.field_Int16);
667 field.SetValue (instance, SByte.MaxValue);
668 Assert.AreEqual (SByte.MaxValue, instance.field_Int16);
669 Throws (field, instance, true);
670 Throws (field, instance, Char.MaxValue);
671 field.SetValue (instance, Int16.MaxValue);
672 Assert.AreEqual (Int16.MaxValue, instance.field_Int16);
673 Throws (field, instance, UInt16.MaxValue);
674 Throws (field, instance, Int32.MaxValue);
675 Throws (field, instance, UInt32.MaxValue);
676 Throws (field, instance, Int64.MaxValue);
677 Throws (field, instance, UInt64.MaxValue);
678 Throws (field, instance, Single.MaxValue);
679 Throws (field, instance, Double.MaxValue);
680 Throws (field, instance, IntPtr.Zero);
681 Throws (field, instance, UIntPtr.Zero);
682 Throws (field, instance, Decimal.MaxValue);
683 Throws (field, instance, DateTime.MaxValue);
684 field.SetValue (instance, ByteEnum.MaxValue);
685 Assert.AreEqual (Byte.MaxValue, instance.field_Int16);
686 field.SetValue (instance, SByteEnum.MaxValue);
687 Assert.AreEqual (SByte.MaxValue, instance.field_Int16);
688 field.SetValue (instance, Int16Enum.MaxValue);
689 Assert.AreEqual (Int16.MaxValue, instance.field_Int16);
690 Throws (field, instance, UInt16Enum.MaxValue);
691 Throws (field, instance, Int32Enum.MaxValue);
692 Throws (field, instance, UInt32Enum.MaxValue);
693 Throws (field, instance, Int64Enum.MaxValue);
694 Throws (field, instance, UInt64Enum.MaxValue);
695 field = fh.GetField ("field_UInt16");
696 field.SetValue (instance, Byte.MaxValue);
697 Assert.AreEqual (Byte.MaxValue, instance.field_UInt16);
698 Throws (field, instance, SByte.MaxValue);
699 Throws (field, instance, true);
700 field.SetValue (instance, Char.MaxValue);
701 Assert.AreEqual (Char.MaxValue, instance.field_UInt16);
702 Throws (field, instance, Int16.MaxValue);
703 field.SetValue (instance, UInt16.MaxValue);
704 Assert.AreEqual (UInt16.MaxValue, instance.field_UInt16);
705 Throws (field, instance, Int32.MaxValue);
706 Throws (field, instance, UInt32.MaxValue);
707 Throws (field, instance, Int64.MaxValue);
708 Throws (field, instance, UInt64.MaxValue);
709 Throws (field, instance, Single.MaxValue);
710 Throws (field, instance, Double.MaxValue);
711 Throws (field, instance, IntPtr.Zero);
712 Throws (field, instance, UIntPtr.Zero);
713 Throws (field, instance, Decimal.MaxValue);
714 Throws (field, instance, DateTime.MaxValue);
715 field.SetValue (instance, ByteEnum.MaxValue);
716 Assert.AreEqual (Byte.MaxValue, instance.field_UInt16);
717 Throws (field, instance, SByteEnum.MaxValue);
718 Throws (field, instance, Int16Enum.MaxValue);
719 field.SetValue (instance, UInt16Enum.MaxValue);
720 Assert.AreEqual (UInt16.MaxValue, instance.field_UInt16);
721 Throws (field, instance, Int32Enum.MaxValue);
722 Throws (field, instance, UInt32Enum.MaxValue);
723 Throws (field, instance, Int64Enum.MaxValue);
724 Throws (field, instance, UInt64Enum.MaxValue);
725 field = fh.GetField ("field_Int32");
726 field.SetValue (instance, Byte.MaxValue);
727 Assert.AreEqual (Byte.MaxValue, instance.field_Int32);
728 field.SetValue (instance, SByte.MaxValue);
729 Assert.AreEqual (SByte.MaxValue, instance.field_Int32);
730 Throws (field, instance, true);
731 field.SetValue (instance, Char.MaxValue);
732 Assert.AreEqual (Char.MaxValue, instance.field_Int32);
733 field.SetValue (instance, Int16.MaxValue);
734 Assert.AreEqual (Int16.MaxValue, instance.field_Int32);
735 field.SetValue (instance, UInt16.MaxValue);
736 Assert.AreEqual (UInt16.MaxValue, instance.field_Int32);
737 field.SetValue (instance, Int32.MaxValue);
738 Assert.AreEqual (Int32.MaxValue, instance.field_Int32);
739 Throws (field, instance, UInt32.MaxValue);
740 Throws (field, instance, Int64.MaxValue);
741 Throws (field, instance, UInt64.MaxValue);
742 Throws (field, instance, Single.MaxValue);
743 Throws (field, instance, Double.MaxValue);
744 Throws (field, instance, IntPtr.Zero);
745 Throws (field, instance, UIntPtr.Zero);
746 Throws (field, instance, Decimal.MaxValue);
747 Throws (field, instance, DateTime.MaxValue);
748 field.SetValue (instance, ByteEnum.MaxValue);
749 Assert.AreEqual (Byte.MaxValue, instance.field_Int32);
750 field.SetValue (instance, SByteEnum.MaxValue);
751 Assert.AreEqual (SByte.MaxValue, instance.field_Int32);
752 field.SetValue (instance, Int16Enum.MaxValue);
753 Assert.AreEqual (Int16.MaxValue, instance.field_Int32);
754 field.SetValue (instance, UInt16Enum.MaxValue);
755 Assert.AreEqual (UInt16.MaxValue, instance.field_Int32);
756 field.SetValue (instance, Int32Enum.MaxValue);
757 Assert.AreEqual (Int32.MaxValue, instance.field_Int32);
758 Throws (field, instance, UInt32Enum.MaxValue);
759 Throws (field, instance, Int64Enum.MaxValue);
760 Throws (field, instance, UInt64Enum.MaxValue);
761 field = fh.GetField ("field_UInt32");
762 field.SetValue (instance, Byte.MaxValue);
763 Assert.AreEqual (Byte.MaxValue, instance.field_UInt32);
764 Throws (field, instance, SByte.MaxValue);
765 Throws (field, instance, true);
766 field.SetValue (instance, Char.MaxValue);
767 Assert.AreEqual (Char.MaxValue, instance.field_UInt32);
768 Throws (field, instance, Int16.MaxValue);
769 field.SetValue (instance, UInt16.MaxValue);
770 Assert.AreEqual (UInt16.MaxValue, instance.field_UInt32);
771 Throws (field, instance, Int32.MaxValue);
772 field.SetValue (instance, UInt32.MaxValue);
773 Assert.AreEqual (UInt32.MaxValue, instance.field_UInt32);
774 Throws (field, instance, Int64.MaxValue);
775 Throws (field, instance, UInt64.MaxValue);
776 Throws (field, instance, Single.MaxValue);
777 Throws (field, instance, Double.MaxValue);
778 Throws (field, instance, IntPtr.Zero);
779 Throws (field, instance, UIntPtr.Zero);
780 Throws (field, instance, Decimal.MaxValue);
781 Throws (field, instance, DateTime.MaxValue);
782 field.SetValue (instance, ByteEnum.MaxValue);
783 Assert.AreEqual (Byte.MaxValue, instance.field_UInt32);
784 Throws (field, instance, SByteEnum.MaxValue);
785 Throws (field, instance, Int16Enum.MaxValue);
786 field.SetValue (instance, UInt16Enum.MaxValue);
787 Assert.AreEqual (UInt16.MaxValue, instance.field_UInt32);
788 Throws (field, instance, Int32Enum.MaxValue);
789 field.SetValue (instance, UInt32Enum.MaxValue);
790 Assert.AreEqual (UInt32.MaxValue, instance.field_UInt32);
791 Throws (field, instance, Int64Enum.MaxValue);
792 Throws (field, instance, UInt64Enum.MaxValue);
793 field = fh.GetField ("field_Int64");
794 field.SetValue (instance, Byte.MaxValue);
795 Assert.AreEqual (Byte.MaxValue, instance.field_Int64);
796 field.SetValue (instance, SByte.MaxValue);
797 Assert.AreEqual (SByte.MaxValue, instance.field_Int64);
798 Throws (field, instance, true);
799 field.SetValue (instance, Char.MaxValue);
800 Assert.AreEqual (Char.MaxValue, instance.field_Int64);
801 field.SetValue (instance, Int16.MaxValue);
802 Assert.AreEqual (Int16.MaxValue, instance.field_Int64);
803 field.SetValue (instance, UInt16.MaxValue);
804 Assert.AreEqual (UInt16.MaxValue, instance.field_Int64);
805 field.SetValue (instance, Int32.MaxValue);
806 Assert.AreEqual (Int32.MaxValue, instance.field_Int64);
807 field.SetValue (instance, UInt32.MaxValue);
808 Assert.AreEqual (UInt32.MaxValue, instance.field_Int64);
809 field.SetValue (instance, Int64.MaxValue);
810 Assert.AreEqual (Int64.MaxValue, instance.field_Int64);
811 Throws (field, instance, UInt64.MaxValue);
812 Throws (field, instance, Single.MaxValue);
813 Throws (field, instance, Double.MaxValue);
814 Throws (field, instance, IntPtr.Zero);
815 Throws (field, instance, UIntPtr.Zero);
816 Throws (field, instance, Decimal.MaxValue);
817 Throws (field, instance, DateTime.MaxValue);
818 field.SetValue (instance, ByteEnum.MaxValue);
819 Assert.AreEqual (Byte.MaxValue, instance.field_Int64);
820 field.SetValue (instance, SByteEnum.MaxValue);
821 Assert.AreEqual (SByte.MaxValue, instance.field_Int64);
822 field.SetValue (instance, Int16Enum.MaxValue);
823 Assert.AreEqual (Int16.MaxValue, instance.field_Int64);
824 field.SetValue (instance, UInt16Enum.MaxValue);
825 Assert.AreEqual (UInt16.MaxValue, instance.field_Int64);
826 field.SetValue (instance, Int32Enum.MaxValue);
827 Assert.AreEqual (Int32.MaxValue, instance.field_Int64);
828 field.SetValue (instance, UInt32Enum.MaxValue);
829 Assert.AreEqual (UInt32.MaxValue, instance.field_Int64);
830 field.SetValue (instance, Int64Enum.MaxValue);
831 Assert.AreEqual (Int64.MaxValue, instance.field_Int64);
832 Throws (field, instance, UInt64Enum.MaxValue);
833 field = fh.GetField ("field_UInt64");
834 field.SetValue (instance, Byte.MaxValue);
835 Assert.AreEqual (Byte.MaxValue, instance.field_UInt64);
836 Throws (field, instance, SByte.MaxValue);
837 Throws (field, instance, true);
838 field.SetValue (instance, Char.MaxValue);
839 Assert.AreEqual (Char.MaxValue, instance.field_UInt64);
840 Throws (field, instance, Int16.MaxValue);
841 field.SetValue (instance, UInt16.MaxValue);
842 Assert.AreEqual (UInt16.MaxValue, instance.field_UInt64);
843 Throws (field, instance, Int32.MaxValue);
844 field.SetValue (instance, UInt32.MaxValue);
845 Assert.AreEqual (UInt32.MaxValue, instance.field_UInt64);
846 Throws (field, instance, Int64.MaxValue);
847 field.SetValue (instance, UInt64.MaxValue);
848 Assert.AreEqual (UInt64.MaxValue, instance.field_UInt64);
849 Throws (field, instance, Single.MaxValue);
850 Throws (field, instance, Double.MaxValue);
851 Throws (field, instance, IntPtr.Zero);
852 Throws (field, instance, UIntPtr.Zero);
853 Throws (field, instance, Decimal.MaxValue);
854 Throws (field, instance, DateTime.MaxValue);
855 field.SetValue (instance, ByteEnum.MaxValue);
856 Assert.AreEqual (Byte.MaxValue, instance.field_UInt64);
857 Throws (field, instance, SByteEnum.MaxValue);
858 Throws (field, instance, Int16Enum.MaxValue);
859 field.SetValue (instance, UInt16Enum.MaxValue);
860 Assert.AreEqual (UInt16.MaxValue, instance.field_UInt64);
861 Throws (field, instance, Int32Enum.MaxValue);
862 field.SetValue (instance, UInt32Enum.MaxValue);
863 Assert.AreEqual (UInt32.MaxValue, instance.field_UInt64);
864 Throws (field, instance, Int64Enum.MaxValue);
865 field.SetValue (instance, UInt64Enum.MaxValue);
866 Assert.AreEqual (UInt64.MaxValue, instance.field_UInt64);
867 field = fh.GetField ("field_Single");
868 field.SetValue (instance, Byte.MaxValue);
869 Assert.AreEqual (Byte.MaxValue, instance.field_Single);
870 field.SetValue (instance, SByte.MaxValue);
871 Assert.AreEqual (SByte.MaxValue, instance.field_Single);
872 Throws (field, instance, true);
873 field.SetValue (instance, Char.MaxValue);
874 Assert.AreEqual ((Single) Char.MaxValue, instance.field_Single);
875 field.SetValue (instance, Int16.MaxValue);
876 Assert.AreEqual (Int16.MaxValue, instance.field_Single);
877 field.SetValue (instance, UInt16.MaxValue);
878 Assert.AreEqual (UInt16.MaxValue, instance.field_Single);
879 field.SetValue (instance, Int32.MaxValue);
880 Assert.AreEqual ((Single)Int32.MaxValue, instance.field_Single);
881 field.SetValue (instance, UInt32.MaxValue);
882 Assert.AreEqual ((Single) UInt32.MaxValue, instance.field_Single);
883 field.SetValue (instance, Int64.MaxValue);
884 Assert.AreEqual (Int64.MaxValue, instance.field_Single);
885 field.SetValue (instance, UInt64.MaxValue);
886 Assert.AreEqual (UInt64.MaxValue, instance.field_Single);
887 field.SetValue (instance, Single.MaxValue);
888 Assert.AreEqual (Single.MaxValue, instance.field_Single);
889 Throws (field, instance, Double.MaxValue);
890 Throws (field, instance, IntPtr.Zero);
891 Throws (field, instance, UIntPtr.Zero);
892 Throws (field, instance, Decimal.MaxValue);
893 Throws (field, instance, DateTime.MaxValue);
894 field.SetValue (instance, ByteEnum.MaxValue);
895 Assert.AreEqual (Byte.MaxValue, instance.field_Single);
896 field.SetValue (instance, SByteEnum.MaxValue);
897 Assert.AreEqual (SByte.MaxValue, instance.field_Single);
898 field.SetValue (instance, Int16Enum.MaxValue);
899 Assert.AreEqual (Int16.MaxValue, instance.field_Single);
900 field.SetValue (instance, UInt16Enum.MaxValue);
901 Assert.AreEqual (UInt16.MaxValue, instance.field_Single);
902 field.SetValue (instance, Int32Enum.MaxValue);
903 Assert.AreEqual ((Single) Int32.MaxValue, instance.field_Single);
904 field.SetValue (instance, UInt32Enum.MaxValue);
905 Assert.AreEqual ((Single) UInt32.MaxValue, instance.field_Single);
906 field.SetValue (instance, Int64Enum.MaxValue);
907 Assert.AreEqual (Int64.MaxValue, instance.field_Single);
908 field.SetValue (instance, UInt64Enum.MaxValue);
909 Assert.AreEqual (UInt64.MaxValue, instance.field_Single);
910 field = fh.GetField ("field_Double");
911 field.SetValue (instance, Byte.MaxValue);
912 Assert.AreEqual (Byte.MaxValue, instance.field_Double);
913 field.SetValue (instance, SByte.MaxValue);
914 Assert.AreEqual (SByte.MaxValue, instance.field_Double);
915 Throws (field, instance, true);
916 field.SetValue (instance, Char.MaxValue);
917 Assert.AreEqual ((Double) Char.MaxValue, instance.field_Double);
918 field.SetValue (instance, Int16.MaxValue);
919 Assert.AreEqual (Int16.MaxValue, instance.field_Double);
920 field.SetValue (instance, UInt16.MaxValue);
921 Assert.AreEqual (UInt16.MaxValue, instance.field_Double);
922 field.SetValue (instance, Int32.MaxValue);
923 Assert.AreEqual (Int32.MaxValue, instance.field_Double);
924 field.SetValue (instance, UInt32.MaxValue);
925 Assert.AreEqual (UInt32.MaxValue, instance.field_Double);
926 field.SetValue (instance, Int64.MaxValue);
927 Assert.AreEqual (Int64.MaxValue, instance.field_Double);
928 field.SetValue (instance, UInt64.MaxValue);
929 Assert.AreEqual (UInt64.MaxValue, instance.field_Double);
930 field.SetValue (instance, Single.MaxValue);
931 Assert.AreEqual (Single.MaxValue, instance.field_Double);
932 field.SetValue (instance, Double.MaxValue);
933 Assert.AreEqual (Double.MaxValue, instance.field_Double);
934 Throws (field, instance, IntPtr.Zero);
935 Throws (field, instance, UIntPtr.Zero);
936 Throws (field, instance, Decimal.MaxValue);
937 Throws (field, instance, DateTime.MaxValue);
938 field.SetValue (instance, ByteEnum.MaxValue);
939 Assert.AreEqual (Byte.MaxValue, instance.field_Double);
940 field.SetValue (instance, SByteEnum.MaxValue);
941 Assert.AreEqual (SByte.MaxValue, instance.field_Double);
942 field.SetValue (instance, Int16Enum.MaxValue);
943 Assert.AreEqual (Int16.MaxValue, instance.field_Double);
944 field.SetValue (instance, UInt16Enum.MaxValue);
945 Assert.AreEqual (UInt16.MaxValue, instance.field_Double);
946 field.SetValue (instance, Int32Enum.MaxValue);
947 Assert.AreEqual (Int32.MaxValue, instance.field_Double);
948 field.SetValue (instance, UInt32Enum.MaxValue);
949 Assert.AreEqual (UInt32.MaxValue, instance.field_Double);
950 field.SetValue (instance, Int64Enum.MaxValue);
951 Assert.AreEqual (Int64.MaxValue, instance.field_Double);
952 field.SetValue (instance, UInt64Enum.MaxValue);
953 Assert.AreEqual (UInt64.MaxValue, instance.field_Double);
954 field = fh.GetField ("field_IntPtr");
955 Throws (field, instance, Byte.MaxValue);
956 Throws (field, instance, SByte.MaxValue);
957 Throws (field, instance, true);
958 Throws (field, instance, Char.MaxValue);
959 Throws (field, instance, Int16.MaxValue);
960 Throws (field, instance, UInt16.MaxValue);
961 Throws (field, instance, Int32.MaxValue);
962 Throws (field, instance, UInt32.MaxValue);
963 Throws (field, instance, Int64.MaxValue);
964 Throws (field, instance, UInt64.MaxValue);
965 Throws (field, instance, Single.MaxValue);
966 Throws (field, instance, Double.MaxValue);
967 field.SetValue (instance, IntPtr.Zero);
968 Assert.AreEqual (IntPtr.Zero, instance.field_IntPtr);
969 Throws (field, instance, UIntPtr.Zero);
970 Throws (field, instance, Decimal.MaxValue);
971 Throws (field, instance, DateTime.MaxValue);
972 Throws (field, instance, ByteEnum.MaxValue);
973 Throws (field, instance, SByteEnum.MaxValue);
974 Throws (field, instance, Int16Enum.MaxValue);
975 Throws (field, instance, UInt16Enum.MaxValue);
976 Throws (field, instance, Int32Enum.MaxValue);
977 Throws (field, instance, UInt32Enum.MaxValue);
978 Throws (field, instance, Int64Enum.MaxValue);
979 Throws (field, instance, UInt64Enum.MaxValue);
980 field = fh.GetField ("field_UIntPtr");
981 Throws (field, instance, Byte.MaxValue);
982 Throws (field, instance, SByte.MaxValue);
983 Throws (field, instance, true);
984 Throws (field, instance, Char.MaxValue);
985 Throws (field, instance, Int16.MaxValue);
986 Throws (field, instance, UInt16.MaxValue);
987 Throws (field, instance, Int32.MaxValue);
988 Throws (field, instance, UInt32.MaxValue);
989 Throws (field, instance, Int64.MaxValue);
990 Throws (field, instance, UInt64.MaxValue);
991 Throws (field, instance, Single.MaxValue);
992 Throws (field, instance, Double.MaxValue);
993 Throws (field, instance, IntPtr.Zero);
994 field.SetValue (instance, UIntPtr.Zero);
995 Assert.AreEqual (UIntPtr.Zero, instance.field_UIntPtr);
996 Throws (field, instance, Decimal.MaxValue);
997 Throws (field, instance, DateTime.MaxValue);
998 Throws (field, instance, ByteEnum.MaxValue);
999 Throws (field, instance, SByteEnum.MaxValue);
1000 Throws (field, instance, Int16Enum.MaxValue);
1001 Throws (field, instance, UInt16Enum.MaxValue);
1002 Throws (field, instance, Int32Enum.MaxValue);
1003 Throws (field, instance, UInt32Enum.MaxValue);
1004 Throws (field, instance, Int64Enum.MaxValue);
1005 Throws (field, instance, UInt64Enum.MaxValue);
1006 field = fh.GetField ("field_Decimal");
1007 Throws (field, instance, Byte.MaxValue);
1008 Throws (field, instance, SByte.MaxValue);
1009 Throws (field, instance, true);
1010 Throws (field, instance, Char.MaxValue);
1011 Throws (field, instance, Int16.MaxValue);
1012 Throws (field, instance, UInt16.MaxValue);
1013 Throws (field, instance, Int32.MaxValue);
1014 Throws (field, instance, UInt32.MaxValue);
1015 Throws (field, instance, Int64.MaxValue);
1016 Throws (field, instance, UInt64.MaxValue);
1017 Throws (field, instance, Single.MaxValue);
1018 Throws (field, instance, Double.MaxValue);
1019 Throws (field, instance, IntPtr.Zero);
1020 Throws (field, instance, UIntPtr.Zero);
1021 field.SetValue (instance, Decimal.MaxValue);
1022 Assert.AreEqual (Decimal.MaxValue, instance.field_Decimal);
1023 Throws (field, instance, DateTime.MaxValue);
1024 Throws (field, instance, ByteEnum.MaxValue);
1025 Throws (field, instance, SByteEnum.MaxValue);
1026 Throws (field, instance, Int16Enum.MaxValue);
1027 Throws (field, instance, UInt16Enum.MaxValue);
1028 Throws (field, instance, Int32Enum.MaxValue);
1029 Throws (field, instance, UInt32Enum.MaxValue);
1030 Throws (field, instance, Int64Enum.MaxValue);
1031 Throws (field, instance, UInt64Enum.MaxValue);
1032 field = fh.GetField ("field_DateTime");
1033 Throws (field, instance, Byte.MaxValue);
1034 Throws (field, instance, SByte.MaxValue);
1035 Throws (field, instance, true);
1036 Throws (field, instance, Char.MaxValue);
1037 Throws (field, instance, Int16.MaxValue);
1038 Throws (field, instance, UInt16.MaxValue);
1039 Throws (field, instance, Int32.MaxValue);
1040 Throws (field, instance, UInt32.MaxValue);
1041 Throws (field, instance, Int64.MaxValue);
1042 Throws (field, instance, UInt64.MaxValue);
1043 Throws (field, instance, Single.MaxValue);
1044 Throws (field, instance, Double.MaxValue);
1045 Throws (field, instance, IntPtr.Zero);
1046 Throws (field, instance, UIntPtr.Zero);
1047 Throws (field, instance, Decimal.MaxValue);
1048 field.SetValue (instance, DateTime.MaxValue);
1049 Assert.AreEqual (DateTime.MaxValue, instance.field_DateTime);
1050 Throws (field, instance, ByteEnum.MaxValue);
1051 Throws (field, instance, SByteEnum.MaxValue);
1052 Throws (field, instance, Int16Enum.MaxValue);
1053 Throws (field, instance, UInt16Enum.MaxValue);
1054 Throws (field, instance, Int32Enum.MaxValue);
1055 Throws (field, instance, UInt32Enum.MaxValue);
1056 Throws (field, instance, Int64Enum.MaxValue);
1057 Throws (field, instance, UInt64Enum.MaxValue);
1058 field = fh.GetField ("field_ByteEnum");
1059 field.SetValue (instance, Byte.MaxValue);
1060 Assert.AreEqual (ByteEnum.MaxValue, instance.field_ByteEnum);
1061 Throws (field, instance, SByte.MaxValue);
1062 Throws (field, instance, true);
1063 Throws (field, instance, Char.MaxValue);
1064 Throws (field, instance, Int16.MaxValue);
1065 Throws (field, instance, UInt16.MaxValue);
1066 Throws (field, instance, Int32.MaxValue);
1067 Throws (field, instance, UInt32.MaxValue);
1068 Throws (field, instance, Int64.MaxValue);
1069 Throws (field, instance, UInt64.MaxValue);
1070 Throws (field, instance, Single.MaxValue);
1071 Throws (field, instance, Double.MaxValue);
1072 Throws (field, instance, IntPtr.Zero);
1073 Throws (field, instance, UIntPtr.Zero);
1074 Throws (field, instance, Decimal.MaxValue);
1075 Throws (field, instance, DateTime.MaxValue);
1076 field.SetValue (instance, ByteEnum.MaxValue);
1077 Assert.AreEqual (ByteEnum.MaxValue, instance.field_ByteEnum);
1078 Throws (field, instance, SByteEnum.MaxValue);
1079 Throws (field, instance, Int16Enum.MaxValue);
1080 Throws (field, instance, UInt16Enum.MaxValue);
1081 Throws (field, instance, Int32Enum.MaxValue);
1082 Throws (field, instance, UInt32Enum.MaxValue);
1083 Throws (field, instance, Int64Enum.MaxValue);
1084 Throws (field, instance, UInt64Enum.MaxValue);
1085 field = fh.GetField ("field_SByteEnum");
1086 Throws (field, instance, Byte.MaxValue);
1087 field.SetValue (instance, SByte.MaxValue);
1088 Assert.AreEqual (SByteEnum.MaxValue, instance.field_SByteEnum);
1089 Throws (field, instance, true);
1090 Throws (field, instance, Char.MaxValue);
1091 Throws (field, instance, Int16.MaxValue);
1092 Throws (field, instance, UInt16.MaxValue);
1093 Throws (field, instance, Int32.MaxValue);
1094 Throws (field, instance, UInt32.MaxValue);
1095 Throws (field, instance, Int64.MaxValue);
1096 Throws (field, instance, UInt64.MaxValue);
1097 Throws (field, instance, Single.MaxValue);
1098 Throws (field, instance, Double.MaxValue);
1099 Throws (field, instance, IntPtr.Zero);
1100 Throws (field, instance, UIntPtr.Zero);
1101 Throws (field, instance, Decimal.MaxValue);
1102 Throws (field, instance, DateTime.MaxValue);
1103 Throws (field, instance, ByteEnum.MaxValue);
1104 field.SetValue (instance, SByteEnum.MaxValue);
1105 Assert.AreEqual (SByteEnum.MaxValue, instance.field_SByteEnum);
1106 Throws (field, instance, Int16Enum.MaxValue);
1107 Throws (field, instance, UInt16Enum.MaxValue);
1108 Throws (field, instance, Int32Enum.MaxValue);
1109 Throws (field, instance, UInt32Enum.MaxValue);
1110 Throws (field, instance, Int64Enum.MaxValue);
1111 Throws (field, instance, UInt64Enum.MaxValue);
1112 field = fh.GetField ("field_Int16Enum");
1113 field.SetValue (instance, Byte.MaxValue);
1114 Assert.AreEqual (Byte.MaxValue, (byte) instance.field_Int16Enum);
1115 field.SetValue (instance, SByte.MaxValue);
1116 Assert.AreEqual (SByte.MaxValue, (sbyte) instance.field_Int16Enum);
1117 Throws (field, instance, true);
1118 Throws (field, instance, Char.MaxValue);
1119 field.SetValue (instance, Int16.MaxValue);
1120 Assert.AreEqual (Int16Enum.MaxValue, instance.field_Int16Enum);
1121 Throws (field, instance, UInt16.MaxValue);
1122 Throws (field, instance, Int32.MaxValue);
1123 Throws (field, instance, UInt32.MaxValue);
1124 Throws (field, instance, Int64.MaxValue);
1125 Throws (field, instance, UInt64.MaxValue);
1126 Throws (field, instance, Single.MaxValue);
1127 Throws (field, instance, Double.MaxValue);
1128 Throws (field, instance, IntPtr.Zero);
1129 Throws (field, instance, UIntPtr.Zero);
1130 Throws (field, instance, Decimal.MaxValue);
1131 Throws (field, instance, DateTime.MaxValue);
1132 field.SetValue (instance, ByteEnum.MaxValue);
1133 Assert.AreEqual (ByteEnum.MaxValue, (ByteEnum) instance.field_Int16Enum);
1134 field.SetValue (instance, SByteEnum.MaxValue);
1135 Assert.AreEqual (SByteEnum.MaxValue, (SByteEnum) instance.field_Int16Enum);
1136 field.SetValue (instance, Int16Enum.MaxValue);
1137 Assert.AreEqual (Int16Enum.MaxValue, instance.field_Int16Enum);
1138 Throws (field, instance, UInt16Enum.MaxValue);
1139 Throws (field, instance, Int32Enum.MaxValue);
1140 Throws (field, instance, UInt32Enum.MaxValue);
1141 Throws (field, instance, Int64Enum.MaxValue);
1142 Throws (field, instance, UInt64Enum.MaxValue);
1143 field = fh.GetField ("field_UInt16Enum");
1144 field.SetValue (instance, Byte.MaxValue);
1145 Assert.AreEqual (Byte.MaxValue, (byte) instance.field_UInt16Enum);
1146 Throws (field, instance, SByte.MaxValue);
1147 Throws (field, instance, true);
1148 field.SetValue (instance, Char.MaxValue);
1149 Assert.AreEqual (Char.MaxValue, (char) instance.field_UInt16Enum);
1150 Throws (field, instance, Int16.MaxValue);
1151 field.SetValue (instance, UInt16.MaxValue);
1152 Assert.AreEqual (UInt16.MaxValue, (UInt16) instance.field_UInt16Enum);
1153 Throws (field, instance, Int32.MaxValue);
1154 Throws (field, instance, UInt32.MaxValue);
1155 Throws (field, instance, Int64.MaxValue);
1156 Throws (field, instance, UInt64.MaxValue);
1157 Throws (field, instance, Single.MaxValue);
1158 Throws (field, instance, Double.MaxValue);
1159 Throws (field, instance, IntPtr.Zero);
1160 Throws (field, instance, UIntPtr.Zero);
1161 Throws (field, instance, Decimal.MaxValue);
1162 Throws (field, instance, DateTime.MaxValue);
1163 field.SetValue (instance, ByteEnum.MaxValue);
1164 Assert.AreEqual (ByteEnum.MaxValue, (ByteEnum) instance.field_UInt16Enum);
1165 Throws (field, instance, SByteEnum.MaxValue);
1166 Throws (field, instance, Int16Enum.MaxValue);
1167 field.SetValue (instance, UInt16Enum.MaxValue);
1168 Assert.AreEqual (UInt16Enum.MaxValue, instance.field_UInt16Enum);
1169 Throws (field, instance, Int32Enum.MaxValue);
1170 Throws (field, instance, UInt32Enum.MaxValue);
1171 Throws (field, instance, Int64Enum.MaxValue);
1172 Throws (field, instance, UInt64Enum.MaxValue);
1173 field = fh.GetField ("field_Int32Enum");
1174 field.SetValue (instance, Byte.MaxValue);
1175 Assert.AreEqual (Byte.MaxValue, (byte) instance.field_Int32Enum);
1176 field.SetValue (instance, SByte.MaxValue);
1177 Assert.AreEqual (SByte.MaxValue, (sbyte) instance.field_Int32Enum);
1178 Throws (field, instance, true);
1179 field.SetValue (instance, Char.MaxValue);
1180 Assert.AreEqual (Char.MaxValue, (char) instance.field_Int32Enum);
1181 field.SetValue (instance, Int16.MaxValue);
1182 Assert.AreEqual (Int16.MaxValue, (Int16) instance.field_Int32Enum);
1183 field.SetValue (instance, UInt16.MaxValue);
1184 Assert.AreEqual (UInt16.MaxValue, (UInt16) instance.field_Int32Enum);
1185 field.SetValue (instance, Int32.MaxValue);
1186 Assert.AreEqual (Int32.MaxValue, (Int32) instance.field_Int32Enum);
1187 Throws (field, instance, UInt32.MaxValue);
1188 Throws (field, instance, Int64.MaxValue);
1189 Throws (field, instance, UInt64.MaxValue);
1190 Throws (field, instance, Single.MaxValue);
1191 Throws (field, instance, Double.MaxValue);
1192 Throws (field, instance, IntPtr.Zero);
1193 Throws (field, instance, UIntPtr.Zero);
1194 Throws (field, instance, Decimal.MaxValue);
1195 Throws (field, instance, DateTime.MaxValue);
1196 field.SetValue (instance, ByteEnum.MaxValue);
1197 Assert.AreEqual (ByteEnum.MaxValue, (ByteEnum) instance.field_Int32Enum);
1198 field.SetValue (instance, SByteEnum.MaxValue);
1199 Assert.AreEqual (SByteEnum.MaxValue, (SByteEnum) instance.field_Int32Enum);
1200 field.SetValue (instance, Int16Enum.MaxValue);
1201 Assert.AreEqual (Int16Enum.MaxValue, (Int16Enum) instance.field_Int32Enum);
1202 field.SetValue (instance, UInt16Enum.MaxValue);
1203 Assert.AreEqual (UInt16Enum.MaxValue, (UInt16Enum) instance.field_Int32Enum);
1204 field.SetValue (instance, Int32Enum.MaxValue);
1205 Assert.AreEqual (Int32Enum.MaxValue, instance.field_Int32Enum);
1206 Throws (field, instance, UInt32Enum.MaxValue);
1207 Throws (field, instance, Int64Enum.MaxValue);
1208 Throws (field, instance, UInt64Enum.MaxValue);
1209 field = fh.GetField ("field_UInt32Enum");
1210 field.SetValue (instance, Byte.MaxValue);
1211 Assert.AreEqual (Byte.MaxValue, (byte) instance.field_UInt32Enum);
1212 Throws (field, instance, SByte.MaxValue);
1213 Throws (field, instance, true);
1214 field.SetValue (instance, Char.MaxValue);
1215 Assert.AreEqual (Char.MaxValue, (char) instance.field_UInt32Enum);
1216 Throws (field, instance, Int16.MaxValue);
1217 field.SetValue (instance, UInt16.MaxValue);
1218 Assert.AreEqual (UInt16.MaxValue, (UInt16) instance.field_UInt32Enum);
1219 Throws (field, instance, Int32.MaxValue);
1220 field.SetValue (instance, UInt32.MaxValue);
1221 Assert.AreEqual (UInt32.MaxValue, (UInt32) instance.field_UInt32Enum);
1222 Throws (field, instance, Int64.MaxValue);
1223 Throws (field, instance, UInt64.MaxValue);
1224 Throws (field, instance, Single.MaxValue);
1225 Throws (field, instance, Double.MaxValue);
1226 Throws (field, instance, IntPtr.Zero);
1227 Throws (field, instance, UIntPtr.Zero);
1228 Throws (field, instance, Decimal.MaxValue);
1229 Throws (field, instance, DateTime.MaxValue);
1230 field.SetValue (instance, ByteEnum.MaxValue);
1231 Assert.AreEqual (ByteEnum.MaxValue, (ByteEnum) instance.field_UInt32Enum);
1232 Throws (field, instance, SByteEnum.MaxValue);
1233 Throws (field, instance, Int16Enum.MaxValue);
1234 field.SetValue (instance, UInt16Enum.MaxValue);
1235 Assert.AreEqual (UInt16Enum.MaxValue, (UInt16Enum) instance.field_UInt32Enum);
1236 Throws (field, instance, Int32Enum.MaxValue);
1237 field.SetValue (instance, UInt32Enum.MaxValue);
1238 Assert.AreEqual (UInt32Enum.MaxValue, instance.field_UInt32Enum);
1239 Throws (field, instance, Int64Enum.MaxValue);
1240 Throws (field, instance, UInt64Enum.MaxValue);
1241 field = fh.GetField ("field_Int64Enum");
1242 field.SetValue (instance, Byte.MaxValue);
1243 Assert.AreEqual (Byte.MaxValue, (byte) instance.field_Int64Enum);
1244 field.SetValue (instance, SByte.MaxValue);
1245 Assert.AreEqual (SByte.MaxValue, (sbyte) instance.field_Int64Enum);
1246 Throws (field, instance, true);
1247 field.SetValue (instance, Char.MaxValue);
1248 Assert.AreEqual (Char.MaxValue, (char) instance.field_Int64Enum);
1249 field.SetValue (instance, Int16.MaxValue);
1250 Assert.AreEqual (Int16.MaxValue, (Int16) instance.field_Int64Enum);
1251 field.SetValue (instance, UInt16.MaxValue);
1252 Assert.AreEqual (UInt16.MaxValue, (UInt16) instance.field_Int64Enum);
1253 field.SetValue (instance, Int32.MaxValue);
1254 Assert.AreEqual (Int32.MaxValue, (Int32) instance.field_Int64Enum);
1255 field.SetValue (instance, UInt32.MaxValue);
1256 Assert.AreEqual (UInt32.MaxValue, (UInt32) instance.field_Int64Enum);
1257 field.SetValue (instance, Int64.MaxValue);
1258 Assert.AreEqual (Int64.MaxValue, (Int64) instance.field_Int64Enum);
1259 Throws (field, instance, UInt64.MaxValue);
1260 Throws (field, instance, Single.MaxValue);
1261 Throws (field, instance, Double.MaxValue);
1262 Throws (field, instance, IntPtr.Zero);
1263 Throws (field, instance, UIntPtr.Zero);
1264 Throws (field, instance, Decimal.MaxValue);
1265 Throws (field, instance, DateTime.MaxValue);
1266 field.SetValue (instance, ByteEnum.MaxValue);
1267 Assert.AreEqual (ByteEnum.MaxValue, (ByteEnum) instance.field_Int64Enum);
1268 field.SetValue (instance, SByteEnum.MaxValue);
1269 Assert.AreEqual (SByteEnum.MaxValue, (SByteEnum) instance.field_Int64Enum);
1270 field.SetValue (instance, Int16Enum.MaxValue);
1271 Assert.AreEqual (Int16Enum.MaxValue, (Int16Enum) instance.field_Int64Enum);
1272 field.SetValue (instance, UInt16Enum.MaxValue);
1273 Assert.AreEqual (UInt16Enum.MaxValue, (UInt16Enum) instance.field_Int64Enum);
1274 field.SetValue (instance, Int32Enum.MaxValue);
1275 Assert.AreEqual (Int32Enum.MaxValue, (Int32Enum) instance.field_Int64Enum);
1276 field.SetValue (instance, UInt32Enum.MaxValue);
1277 Assert.AreEqual (UInt32Enum.MaxValue, (UInt32Enum) instance.field_Int64Enum);
1278 field.SetValue (instance, Int64Enum.MaxValue);
1279 Assert.AreEqual (Int64Enum.MaxValue, instance.field_Int64Enum);
1280 Throws (field, instance, UInt64Enum.MaxValue);
1281 field = fh.GetField ("field_UInt64Enum");
1282 field.SetValue (instance, Byte.MaxValue);
1283 Assert.AreEqual (Byte.MaxValue, (byte) instance.field_UInt64Enum);
1284 Throws (field, instance, SByte.MaxValue);
1285 Throws (field, instance, true);
1286 field.SetValue (instance, Char.MaxValue);
1287 Assert.AreEqual (Char.MaxValue, (char) instance.field_UInt64Enum);
1288 Throws (field, instance, Int16.MaxValue);
1289 field.SetValue (instance, UInt16.MaxValue);
1290 Assert.AreEqual (UInt16.MaxValue, (UInt16) instance.field_UInt64Enum);
1291 Throws (field, instance, Int32.MaxValue);
1292 field.SetValue (instance, UInt32.MaxValue);
1293 Assert.AreEqual (UInt32.MaxValue, (UInt32) instance.field_UInt64Enum);
1294 Throws (field, instance, Int64.MaxValue);
1295 field.SetValue (instance, UInt64.MaxValue);
1296 Assert.AreEqual (UInt64.MaxValue, (UInt64) instance.field_UInt64Enum);
1297 Throws (field, instance, Single.MaxValue);
1298 Throws (field, instance, Double.MaxValue);
1299 Throws (field, instance, IntPtr.Zero);
1300 Throws (field, instance, UIntPtr.Zero);
1301 Throws (field, instance, Decimal.MaxValue);
1302 Throws (field, instance, DateTime.MaxValue);
1303 field.SetValue (instance, ByteEnum.MaxValue);
1304 Assert.AreEqual (ByteEnum.MaxValue, (ByteEnum) instance.field_UInt64Enum);
1305 Throws (field, instance, SByteEnum.MaxValue);
1306 Throws (field, instance, Int16Enum.MaxValue);
1307 field.SetValue (instance, UInt16Enum.MaxValue);
1308 Assert.AreEqual (UInt16Enum.MaxValue, (UInt16Enum) instance.field_UInt64Enum);
1309 Throws (field, instance, Int32Enum.MaxValue);
1310 field.SetValue (instance, UInt32Enum.MaxValue);
1311 Assert.AreEqual (UInt32Enum.MaxValue, (UInt32Enum) instance.field_UInt64Enum);
1312 Throws (field, instance, Int64Enum.MaxValue);
1313 field.SetValue (instance, UInt64Enum.MaxValue);
1314 Assert.AreEqual (UInt64Enum.MaxValue, instance.field_UInt64Enum);
1318 static void Throws (FieldInfo field, object instance, object value)
1321 field.SetValue (instance, value);
1322 Assert.Fail ("ArgumentException expected");
1323 } catch (ArgumentException ex) {
1327 public object[] ObjectArrayField;
1330 public void TestSetValueArray ()
1332 var field = typeof (FieldInfoTest).GetField ("ObjectArrayField");
1333 var instance = new FieldInfoTest ();
1334 field.SetValue (instance, new string[] { "3" });
1335 field.SetValue (instance, null);
1337 Throws (field, instance, new int[] { 3 });
1341 public int MaxValue;
1346 public void SetValueDirect ()
1348 TestFields fields = new TestFields { MaxValue = 1234, str = "A" };
1350 FieldInfo info = fields.GetType ().GetField ("MaxValue");
1351 TypedReference reference = __makeref(fields);
1352 info.SetValueDirect (reference, 4096);
1353 Assert.AreEqual (4096, fields.MaxValue);
1355 info = fields.GetType ().GetField ("str");
1356 reference = __makeref(fields);
1357 info.SetValueDirect (reference, "B");
1358 Assert.AreEqual ("B", fields.str);
1366 The whole point of this field is to make sure we don't create the vtable layout
1367 when loading the value of constants for Foo<>. See bug #594942.
1371 public static int field;
1372 public const int constant = 10;
1373 public const string sconstant = "waa";
1374 public const IntEnum econstant = IntEnum.Third;
1377 public enum IntEnum {
1383 public enum LongEnum : long {
1389 public const int int_field = 5;
1390 public const long long_field = Int64.MaxValue;
1391 public const IntEnum int_enum_field = IntEnum.Second;
1392 public const LongEnum long_enum_field = LongEnum.Second;
1393 public const string string_field = "Hello";
1394 public const FieldInfoTest object_field = null;
1395 public int non_const_field;
1399 // We do not refernece the field, that is expected
1400 #pragma warning disable 169
1402 class RefOnlyFieldClass
1405 static int RefOnlyField;
1407 #pragma warning restore 169
1409 class NonPublicFieldClass
1411 protected int protectedField;
1413 public void Dummy ()
1419 public class FieldInfoTest<T>