1 // 2 // FieldInfoTest - NUnit Test Cases for the FieldInfo class 3 // 4 // Authors: 5 // Zoltan Varga (vargaz@freemail.hu) 6 // Gert Driesen (drieseng@users.sourceforge.net) 7 // 8 // (c) 2003 Ximian, Inc. (http://www.ximian.com) 9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com) 10 // 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: 18 // 19 // The above copyright notice and this permission notice shall be 20 // included in all copies or substantial portions of the Software. 21 // 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. 29 // 30 31 using System; 32 using System.Threading; 33 using System.Reflection; 34 #if !MONOTOUCH && !FULL_AOT_RUNTIME 35 using System.Reflection.Emit; 36 #endif 37 using System.Runtime.InteropServices; 38 39 using NUnit.Framework; 40 41 namespace MonoTests.System.Reflection 42 { 43 [StructLayout(LayoutKind.Explicit, Pack = 4, Size = 64)] 44 public class Class1 45 { 46 [FieldOffset (32)] 47 public int i; 48 } 49 50 [StructLayout(LayoutKind.Sequential)] 51 public class Class2 52 { 53 [MarshalAsAttribute(UnmanagedType.Bool)] 54 public int f0; 55 56 [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] 57 public string[] f1; 58 59 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)] 60 public string f2; 61 #if FEATURE_COMINTEROP 62 [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof (Marshal1), MarshalCookie="5")] 63 public int f3; 64 65 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] 66 public object f4; 67 #endif 68 [Obsolete] 69 public int f5; 70 } 71 72 public class Class3 : Class2 73 { 74 } 75 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 79 { 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; 97 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; 106 } 107 #pragma warning restore 649 108 109 public enum ByteEnum : byte 110 { 111 MaxValue = Byte.MaxValue 112 } 113 114 public enum SByteEnum : sbyte 115 { 116 MaxValue = SByte.MaxValue 117 } 118 119 public enum Int16Enum : short 120 { 121 MaxValue = Int16.MaxValue 122 } 123 124 public enum UInt16Enum : ushort 125 { 126 MaxValue = UInt16.MaxValue 127 } 128 129 public enum Int32Enum : int 130 { 131 MaxValue = Int32.MaxValue 132 } 133 134 public enum UInt32Enum: uint 135 { 136 MaxValue= UInt32.MaxValue 137 } 138 139 public enum Int64Enum : long 140 { 141 MaxValue = Int64.MaxValue 142 } 143 144 public enum UInt64Enum: ulong 145 { 146 MaxValue = UInt64.MaxValue 147 } 148 149 [TestFixture] 150 public unsafe class FieldInfoTest 151 { 152 [NonSerialized] 153 public int i; 154 155 [Test] IsDefined_AttributeType_Null()156 public void IsDefined_AttributeType_Null () 157 { 158 Type type = typeof (FieldInfoTest); 159 FieldInfo field = type.GetField ("i"); 160 161 try { 162 field.IsDefined ((Type) null, false); 163 Assert.Fail ("#1"); 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"); 170 } 171 } 172 173 [Test] FieldInfoModule()174 public void FieldInfoModule () 175 { 176 Type type = typeof (FieldInfoTest); 177 FieldInfo field = type.GetField ("i"); 178 179 Assert.AreEqual (type.Module, field.Module); 180 } 181 182 [Test] GetCustomAttributes()183 public void GetCustomAttributes () 184 { 185 object [] attrs; 186 FieldInfo fi; 187 188 fi = typeof (Class2).GetField ("f5"); 189 190 attrs = fi.GetCustomAttributes (false); 191 Assert.AreEqual (1, attrs.Length, "#B1"); 192 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#B2"); 193 attrs = fi.GetCustomAttributes (true); 194 Assert.AreEqual (1, attrs.Length, "#B3"); 195 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#B4"); 196 attrs = fi.GetCustomAttributes (typeof (MarshalAsAttribute), false); 197 Assert.AreEqual (0, attrs.Length, "#B5"); 198 attrs = fi.GetCustomAttributes (typeof (MarshalAsAttribute), true); 199 Assert.AreEqual (0, attrs.Length, "#B6"); 200 attrs = fi.GetCustomAttributes (typeof (ObsoleteAttribute), false); 201 Assert.AreEqual (1, attrs.Length, "#B7"); 202 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#B8"); 203 attrs = fi.GetCustomAttributes (typeof (ObsoleteAttribute), true); 204 Assert.AreEqual (1, attrs.Length, "#B9"); 205 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#B10"); 206 207 fi = typeof (Class3).GetField ("f5"); 208 209 attrs = fi.GetCustomAttributes (false); 210 Assert.AreEqual (1, attrs.Length, "#D1"); 211 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#D2"); 212 attrs = fi.GetCustomAttributes (true); 213 Assert.AreEqual (1, attrs.Length, "#D3"); 214 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#D4"); 215 attrs = fi.GetCustomAttributes (typeof (MarshalAsAttribute), false); 216 Assert.AreEqual (0, attrs.Length, "#D5"); 217 attrs = fi.GetCustomAttributes (typeof (MarshalAsAttribute), true); 218 Assert.AreEqual (0, attrs.Length, "#D6"); 219 attrs = fi.GetCustomAttributes (typeof (ObsoleteAttribute), false); 220 Assert.AreEqual (1, attrs.Length, "#D7"); 221 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#D8"); 222 attrs = fi.GetCustomAttributes (typeof (ObsoleteAttribute), true); 223 Assert.AreEqual (1, attrs.Length, "#D9"); 224 Assert.AreEqual (typeof (ObsoleteAttribute), attrs [0].GetType (), "#D10"); 225 } 226 227 [Test] // GetFieldFromHandle (RuntimeFieldHandle) GetFieldFromHandle1_Handle_Zero()228 public void GetFieldFromHandle1_Handle_Zero () 229 { 230 RuntimeFieldHandle fh = new RuntimeFieldHandle (); 231 232 try { 233 FieldInfo.GetFieldFromHandle (fh); 234 Assert.Fail ("#1"); 235 } catch (ArgumentException ex) { 236 // Handle is not initialized 237 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 238 Assert.IsNull (ex.InnerException, "#3"); 239 Assert.IsNotNull (ex.Message, "#4"); 240 Assert.IsNull (ex.ParamName, "#5"); 241 } 242 } 243 244 [Test] // GetFieldFromHandle (RuntimeFieldHandle, RuntimeTypeHandle) GetFieldFromHandle2_DeclaringType_Zero()245 public void GetFieldFromHandle2_DeclaringType_Zero () 246 { 247 RuntimeTypeHandle th = new RuntimeTypeHandle (); 248 FieldInfo fi1 = typeof (Class2).GetField ("f5"); 249 RuntimeFieldHandle fh = fi1.FieldHandle; 250 251 FieldInfo fi2 = FieldInfo.GetFieldFromHandle (fh, th); 252 Assert.IsNotNull (fi2, "#1"); 253 Assert.AreSame (fi1.DeclaringType, fi2.DeclaringType, "#2"); 254 Assert.AreEqual (fi1.FieldType, fi2.FieldType, "#3"); 255 Assert.AreEqual (fi1.Name, fi2.Name, "#4"); 256 } 257 258 [Test] // GetFieldFromHandle (RuntimeFieldHandle, RuntimeTypeHandle) GetFieldFromHandle2_Handle_Generic()259 public void GetFieldFromHandle2_Handle_Generic () 260 { 261 FieldInfoTest<string> instance = new FieldInfoTest<string> (); 262 Type t = instance.GetType (); 263 264 FieldInfo fi1 = t.GetField ("TestField"); 265 RuntimeFieldHandle fh = fi1.FieldHandle; 266 RuntimeTypeHandle th = t.TypeHandle; 267 268 FieldInfo fi2 = FieldInfo.GetFieldFromHandle (fh, th); 269 Assert.IsNotNull (fi2, "#1"); 270 Assert.AreSame (t, fi2.DeclaringType, "#2"); 271 Assert.AreEqual (typeof (string), fi2.FieldType, "#3"); 272 Assert.AreEqual ("TestField", fi2.Name, "#4"); 273 } 274 275 [Test] // GetFieldFromHandle (RuntimeFieldHandle, RuntimeTypeHandle) 276 [Category ("NotWorking")] 277 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=343449 GetFieldFromHandle2_Handle_GenericDefinition()278 public void GetFieldFromHandle2_Handle_GenericDefinition () 279 { 280 Type t1 = typeof (FieldInfoTest<>); 281 FieldInfo fi1 = t1.GetField ("TestField"); 282 RuntimeFieldHandle fh = fi1.FieldHandle; 283 284 FieldInfoTest<string> instance = new FieldInfoTest<string> (); 285 Type t2 = instance.GetType (); 286 RuntimeTypeHandle th = t2.TypeHandle; 287 288 FieldInfo fi2 = FieldInfo.GetFieldFromHandle (fh, th); 289 Assert.IsNotNull (fi2, "#1"); 290 Assert.AreSame (t2, fi2.DeclaringType, "#2"); 291 Assert.AreEqual (typeof (string), fi2.FieldType, "#3"); 292 Assert.AreEqual ("TestField", fi2.Name, "#4"); 293 } 294 295 [Test] // GetFieldFromHandle (RuntimeFieldHandle, RuntimeTypeHandle) GetFieldFromHandle2_Handle_Zero()296 public void GetFieldFromHandle2_Handle_Zero () 297 { 298 object instance = new Class2 (); 299 RuntimeTypeHandle th = Type.GetTypeHandle (instance); 300 RuntimeFieldHandle fh = new RuntimeFieldHandle (); 301 302 try { 303 FieldInfo.GetFieldFromHandle (fh, th); 304 Assert.Fail ("#1"); 305 } catch (ArgumentException ex) { 306 // Handle is not initialized 307 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 308 Assert.IsNull (ex.InnerException, "#3"); 309 Assert.IsNotNull (ex.Message, "#4"); 310 Assert.IsNull (ex.ParamName, "#5"); 311 } 312 } 313 314 [Test] 315 [ExpectedException (typeof (ArgumentException))] GetFieldFromHandle2_Incompatible()316 public void GetFieldFromHandle2_Incompatible () 317 { 318 RuntimeFieldHandle fh = typeof (FieldInfoTest<int>).GetField ("TestField").FieldHandle; 319 320 FieldInfoTest<string> instance = new FieldInfoTest<string> (); 321 Type t2 = instance.GetType (); 322 RuntimeTypeHandle th = t2.TypeHandle; 323 324 FieldInfo fi2 = FieldInfo.GetFieldFromHandle (fh, th); 325 } 326 327 [Test] PseudoCustomAttributes()328 public void PseudoCustomAttributes () 329 { 330 object [] attrs; 331 Type t = typeof (FieldInfoTest); 332 333 Assert.AreEqual (1, t.GetField ("i").GetCustomAttributes (typeof (NonSerializedAttribute), true).Length); 334 335 attrs = typeof (Class1).GetField ("i").GetCustomAttributes (true); 336 Assert.AreEqual (1, attrs.Length, "#B1"); 337 FieldOffsetAttribute field_attr = (FieldOffsetAttribute) attrs [0]; 338 Assert.AreEqual (32, field_attr.Value, "#B2"); 339 340 MarshalAsAttribute attr; 341 342 attrs = typeof (Class2).GetField ("f0").GetCustomAttributes (true); 343 Assert.AreEqual (1, attrs.Length, "#C1"); 344 attr = (MarshalAsAttribute) attrs [0]; 345 Assert.AreEqual (UnmanagedType.Bool, attr.Value, "#C2"); 346 347 attrs = typeof (Class2).GetField ("f1").GetCustomAttributes (true); 348 Assert.AreEqual (1, attrs.Length, "#D1"); 349 attr = (MarshalAsAttribute) attrs [0]; 350 Assert.AreEqual (UnmanagedType.LPArray, attr.Value, "#D2"); 351 Assert.AreEqual (UnmanagedType.LPStr, attr.ArraySubType, "#D3"); 352 353 attrs = typeof (Class2).GetField ("f2").GetCustomAttributes (true); 354 Assert.AreEqual (1, attrs.Length, "#E1"); 355 attr = (MarshalAsAttribute) attrs [0]; 356 Assert.AreEqual (UnmanagedType.ByValTStr, attr.Value, "#E2"); 357 Assert.AreEqual (100, attr.SizeConst, "#E3"); 358 359 #if FEATURE_COMINTEROP 360 attrs = typeof (Class2).GetField ("f3").GetCustomAttributes (true); 361 Assert.AreEqual (1, attrs.Length, "#F1"); 362 attr = (MarshalAsAttribute) attrs [0]; 363 Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#F2"); 364 365 Assert.AreEqual ("5", attr.MarshalCookie, "#F3"); 366 Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#F4"); 367 368 attrs = typeof (Class3).GetField ("f3").GetCustomAttributes (false); 369 Assert.AreEqual (1, attrs.Length, "#G1"); 370 attr = (MarshalAsAttribute) attrs [0]; 371 Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#G2"); 372 Assert.AreEqual ("5", attr.MarshalCookie, "#G3"); 373 Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#G4"); 374 375 attrs = typeof (Class3).GetField ("f3").GetCustomAttributes (true); 376 Assert.AreEqual (1, attrs.Length, "#H1"); 377 attr = (MarshalAsAttribute) attrs [0]; 378 Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#H2"); 379 Assert.AreEqual ("5", attr.MarshalCookie, "#H3"); 380 Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#H4"); 381 382 // bug #82465 383 attrs = typeof (Class2).GetField ("f3").GetCustomAttributes (true); 384 Assert.AreEqual (1, attrs.Length, "#I1"); 385 attr = (MarshalAsAttribute) attrs [0]; 386 Assert.AreEqual (UnmanagedType.CustomMarshaler, attr.Value, "#I2"); 387 Assert.AreEqual ("5", attr.MarshalCookie, "#I3"); 388 Assert.AreEqual (typeof (Marshal1), Type.GetType (attr.MarshalType), "#I4"); 389 #endif 390 } 391 392 // Disable "field not used warning", this is intended. 393 #pragma warning disable 649 394 class Foo { 395 public static int static_field; 396 public int field; 397 } 398 #pragma warning restore 649 399 400 [ExpectedException (typeof (ArgumentException))] GetValueWrongObject()401 public void GetValueWrongObject () 402 { 403 Foo f = new Foo (); 404 405 typeof (Foo).GetField ("field").GetValue (typeof (int)); 406 } 407 GetValueWrongObjectStatic()408 public void GetValueWrongObjectStatic () 409 { 410 Foo f = new Foo (); 411 412 // This is allowed in MS.NET 413 typeof (Foo).GetField ("static_field").GetValue (typeof (int)); 414 } 415 416 [Test] 417 [ExpectedException (typeof (InvalidOperationException))] GetValueOnRefOnlyAssembly()418 public void GetValueOnRefOnlyAssembly () 419 { 420 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName); 421 Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName); 422 FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic); 423 f.GetValue (null); 424 } 425 426 [Test] 427 [ExpectedException (typeof (InvalidOperationException))] SetValueOnRefOnlyAssembly()428 public void SetValueOnRefOnlyAssembly () 429 { 430 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName); 431 Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName); 432 FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic); 433 f.SetValue (null, 8); 434 } 435 436 const int literal = 42; 437 438 [Test] 439 [ExpectedException (typeof (FieldAccessException))] SetValueOnLiteralField()440 public void SetValueOnLiteralField () 441 { 442 FieldInfo f = typeof (FieldInfoTest).GetField ("literal", BindingFlags.Static | BindingFlags.NonPublic); 443 f.SetValue (null, 0); 444 } 445 446 public int? nullable_field; 447 448 public static int? static_nullable_field; 449 450 [Test] NullableTests()451 public void NullableTests () 452 { 453 FieldInfoTest t = new FieldInfoTest (); 454 455 FieldInfo fi = typeof (FieldInfoTest).GetField ("nullable_field"); 456 457 fi.SetValue (t, 101); 458 Assert.AreEqual (101, fi.GetValue (t)); 459 fi.SetValue (t, null); 460 Assert.AreEqual (null, fi.GetValue (t)); 461 462 FieldInfo fi2 = typeof (FieldInfoTest).GetField ("static_nullable_field"); 463 464 fi2.SetValue (t, 101); 465 Assert.AreEqual (101, fi2.GetValue (t)); 466 fi2.SetValue (t, null); 467 Assert.AreEqual (null, fi2.GetValue (t)); 468 } 469 470 [Test] NonPublicTests()471 public void NonPublicTests () 472 { 473 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName); 474 475 Type t = assembly.GetType (typeof (NonPublicFieldClass).FullName); 476 477 // try to get non-public field 478 FieldInfo fi = t.GetField ("protectedField"); 479 Assert.IsNull (fi); 480 // get it for real 481 fi = t.GetField ("protectedField", BindingFlags.NonPublic | BindingFlags.Instance); 482 Assert.IsNotNull (fi); 483 } 484 485 [Test] GetRawDefaultValue()486 public void GetRawDefaultValue () 487 { 488 Assert.AreEqual (5, typeof (FieldInfoTest).GetField ("int_field").GetRawConstantValue ()); 489 Assert.AreEqual (Int64.MaxValue, typeof (FieldInfoTest).GetField ("long_field").GetRawConstantValue ()); 490 Assert.AreEqual (2, typeof (FieldInfoTest).GetField ("int_enum_field").GetRawConstantValue ()); 491 Assert.AreEqual (typeof (int), typeof (FieldInfoTest).GetField ("int_enum_field").GetRawConstantValue ().GetType ()); 492 Assert.AreEqual (2, typeof (FieldInfoTest).GetField ("long_enum_field").GetRawConstantValue ()); 493 Assert.AreEqual (typeof (long), typeof (FieldInfoTest).GetField ("long_enum_field").GetRawConstantValue ().GetType ()); 494 Assert.AreEqual ("Hello", typeof (FieldInfoTest).GetField ("string_field").GetRawConstantValue ()); 495 Assert.AreEqual (null, typeof (FieldInfoTest).GetField ("object_field").GetRawConstantValue ()); 496 } 497 498 [Test] 499 [ExpectedException (typeof (InvalidOperationException))] GetRawDefaultValueNoDefault()500 public void GetRawDefaultValueNoDefault () 501 { 502 typeof (FieldInfoTest).GetField ("non_const_field").GetRawConstantValue (); 503 } 504 505 [Test] 506 [ExpectedException (typeof (InvalidOperationException))] GetValueOpenGeneric()507 public void GetValueOpenGeneric () 508 { 509 typeof(Foo<>).GetField ("field").GetValue (null); 510 } 511 512 [Test] 513 [ExpectedException (typeof (InvalidOperationException))] SetValueOpenGeneric()514 public void SetValueOpenGeneric () 515 { 516 typeof(Foo<>).GetField ("field").SetValue (null, 0); 517 } 518 519 [Test] GetValueOnConstantOfOpenGeneric()520 public void GetValueOnConstantOfOpenGeneric () 521 { 522 Assert.AreEqual (10, typeof(Foo<>).GetField ("constant").GetValue (null), "#1"); 523 Assert.AreEqual ("waa", typeof(Foo<>).GetField ("sconstant").GetValue (null), "#2"); 524 Assert.AreEqual (IntEnum.Third, typeof(Foo<>).GetField ("econstant").GetValue (null), "#3"); 525 } 526 527 public static unsafe void* ip; 528 529 [Test] GetSetValuePointers()530 public unsafe void GetSetValuePointers () 531 { 532 Pointer p0 = (Pointer)typeof (FieldInfoTest).GetField ("ip").GetValue (null); 533 int *p0i = (int*)Pointer.Unbox (p0); 534 Assert.AreEqual (IntPtr.Zero, new IntPtr (p0i)); 535 536 int i = 5; 537 void *p = &i; 538 typeof (FieldInfoTest).GetField ("ip").SetValue (null, (IntPtr)p); 539 Pointer p2 = (Pointer)typeof (FieldInfoTest).GetField ("ip").GetValue (null); 540 541 int *pi = (int*)Pointer.Unbox (p2); 542 Assert.AreEqual (5, *pi); 543 544 typeof (FieldInfoTest).GetField ("ip").SetValue (null, (UIntPtr)p); 545 p2 = (Pointer)typeof (FieldInfoTest).GetField ("ip").GetValue (null); 546 547 pi = (int*)Pointer.Unbox (p2); 548 Assert.AreEqual (5, *pi); 549 } 550 551 [Test] SetValuePrimitiveConversions()552 public void SetValuePrimitiveConversions () 553 { 554 FieldInfo field; 555 var instance = new FieldInvokeMatrix (); 556 var fh = typeof (FieldInvokeMatrix); 557 558 field = fh.GetField ("field_Byte"); 559 field.SetValue (instance, Byte.MaxValue); 560 Assert.AreEqual (Byte.MaxValue, instance.field_Byte); 561 Throws (field, instance, SByte.MaxValue); 562 Throws (field, instance, true); 563 Throws (field, instance, Char.MaxValue); 564 Throws (field, instance, Int16.MaxValue); 565 Throws (field, instance, UInt16.MaxValue); 566 Throws (field, instance, Int32.MaxValue); 567 Throws (field, instance, UInt32.MaxValue); 568 Throws (field, instance, Int64.MaxValue); 569 Throws (field, instance, UInt64.MaxValue); 570 Throws (field, instance, Single.MaxValue); 571 Throws (field, instance, Double.MaxValue); 572 Throws (field, instance, IntPtr.Zero); 573 Throws (field, instance, UIntPtr.Zero); 574 Throws (field, instance, Decimal.MaxValue); 575 Throws (field, instance, DateTime.MaxValue); 576 field.SetValue (instance, ByteEnum.MaxValue); 577 Assert.AreEqual (Byte.MaxValue, instance.field_Byte); 578 Throws (field, instance, SByteEnum.MaxValue); 579 Throws (field, instance, Int16Enum.MaxValue); 580 Throws (field, instance, UInt16Enum.MaxValue); 581 Throws (field, instance, Int32Enum.MaxValue); 582 Throws (field, instance, UInt32Enum.MaxValue); 583 Throws (field, instance, Int64Enum.MaxValue); 584 Throws (field, instance, UInt64Enum.MaxValue); 585 field = fh.GetField ("field_SByte"); 586 Throws (field, instance, Byte.MaxValue); 587 field.SetValue (instance, SByte.MaxValue); 588 Assert.AreEqual (SByte.MaxValue, instance.field_SByte); 589 Throws (field, instance, true); 590 Throws (field, instance, Char.MaxValue); 591 Throws (field, instance, Int16.MaxValue); 592 Throws (field, instance, UInt16.MaxValue); 593 Throws (field, instance, Int32.MaxValue); 594 Throws (field, instance, UInt32.MaxValue); 595 Throws (field, instance, Int64.MaxValue); 596 Throws (field, instance, UInt64.MaxValue); 597 Throws (field, instance, Single.MaxValue); 598 Throws (field, instance, Double.MaxValue); 599 Throws (field, instance, IntPtr.Zero); 600 Throws (field, instance, UIntPtr.Zero); 601 Throws (field, instance, Decimal.MaxValue); 602 Throws (field, instance, DateTime.MaxValue); 603 Throws (field, instance, ByteEnum.MaxValue); 604 field.SetValue (instance, SByteEnum.MaxValue); 605 Assert.AreEqual (SByte.MaxValue, instance.field_SByte); 606 Throws (field, instance, Int16Enum.MaxValue); 607 Throws (field, instance, UInt16Enum.MaxValue); 608 Throws (field, instance, Int32Enum.MaxValue); 609 Throws (field, instance, UInt32Enum.MaxValue); 610 Throws (field, instance, Int64Enum.MaxValue); 611 Throws (field, instance, UInt64Enum.MaxValue); 612 field = fh.GetField ("field_Boolean"); 613 Throws (field, instance, Byte.MaxValue); 614 Throws (field, instance, SByte.MaxValue); 615 field.SetValue (instance, true); 616 Assert.AreEqual (true, instance.field_Boolean); 617 Throws (field, instance, Char.MaxValue); 618 Throws (field, instance, Int16.MaxValue); 619 Throws (field, instance, UInt16.MaxValue); 620 Throws (field, instance, Int32.MaxValue); 621 Throws (field, instance, UInt32.MaxValue); 622 Throws (field, instance, Int64.MaxValue); 623 Throws (field, instance, UInt64.MaxValue); 624 Throws (field, instance, Single.MaxValue); 625 Throws (field, instance, Double.MaxValue); 626 Throws (field, instance, IntPtr.Zero); 627 Throws (field, instance, UIntPtr.Zero); 628 Throws (field, instance, Decimal.MaxValue); 629 Throws (field, instance, DateTime.MaxValue); 630 Throws (field, instance, ByteEnum.MaxValue); 631 Throws (field, instance, SByteEnum.MaxValue); 632 Throws (field, instance, Int16Enum.MaxValue); 633 Throws (field, instance, UInt16Enum.MaxValue); 634 Throws (field, instance, Int32Enum.MaxValue); 635 Throws (field, instance, UInt32Enum.MaxValue); 636 Throws (field, instance, Int64Enum.MaxValue); 637 Throws (field, instance, UInt64Enum.MaxValue); 638 field = fh.GetField ("field_Char"); 639 field.SetValue (instance, Byte.MaxValue); 640 Assert.AreEqual (Byte.MaxValue, instance.field_Char); 641 Throws (field, instance, SByte.MaxValue); 642 Throws (field, instance, true); 643 field.SetValue (instance, Char.MaxValue); 644 Assert.AreEqual (Char.MaxValue, instance.field_Char); 645 Throws (field, instance, Int16.MaxValue); 646 field.SetValue (instance, UInt16.MaxValue); 647 Assert.AreEqual (UInt16.MaxValue, instance.field_Char); 648 Throws (field, instance, Int32.MaxValue); 649 Throws (field, instance, UInt32.MaxValue); 650 Throws (field, instance, Int64.MaxValue); 651 Throws (field, instance, UInt64.MaxValue); 652 Throws (field, instance, Single.MaxValue); 653 Throws (field, instance, Double.MaxValue); 654 Throws (field, instance, IntPtr.Zero); 655 Throws (field, instance, UIntPtr.Zero); 656 Throws (field, instance, Decimal.MaxValue); 657 Throws (field, instance, DateTime.MaxValue); 658 field.SetValue (instance, ByteEnum.MaxValue); 659 Assert.AreEqual (Byte.MaxValue, instance.field_Char); 660 Throws (field, instance, SByteEnum.MaxValue); 661 Throws (field, instance, Int16Enum.MaxValue); 662 field.SetValue (instance, UInt16Enum.MaxValue); 663 Assert.AreEqual (UInt16.MaxValue, instance.field_Char); 664 Throws (field, instance, Int32Enum.MaxValue); 665 Throws (field, instance, UInt32Enum.MaxValue); 666 Throws (field, instance, Int64Enum.MaxValue); 667 Throws (field, instance, UInt64Enum.MaxValue); 668 field = fh.GetField ("field_Int16"); 669 field.SetValue (instance, Byte.MaxValue); 670 Assert.AreEqual (Byte.MaxValue, instance.field_Int16); 671 field.SetValue (instance, SByte.MaxValue); 672 Assert.AreEqual (SByte.MaxValue, instance.field_Int16); 673 Throws (field, instance, true); 674 Throws (field, instance, Char.MaxValue); 675 field.SetValue (instance, Int16.MaxValue); 676 Assert.AreEqual (Int16.MaxValue, instance.field_Int16); 677 Throws (field, instance, UInt16.MaxValue); 678 Throws (field, instance, Int32.MaxValue); 679 Throws (field, instance, UInt32.MaxValue); 680 Throws (field, instance, Int64.MaxValue); 681 Throws (field, instance, UInt64.MaxValue); 682 Throws (field, instance, Single.MaxValue); 683 Throws (field, instance, Double.MaxValue); 684 Throws (field, instance, IntPtr.Zero); 685 Throws (field, instance, UIntPtr.Zero); 686 Throws (field, instance, Decimal.MaxValue); 687 Throws (field, instance, DateTime.MaxValue); 688 field.SetValue (instance, ByteEnum.MaxValue); 689 Assert.AreEqual (Byte.MaxValue, instance.field_Int16); 690 field.SetValue (instance, SByteEnum.MaxValue); 691 Assert.AreEqual (SByte.MaxValue, instance.field_Int16); 692 field.SetValue (instance, Int16Enum.MaxValue); 693 Assert.AreEqual (Int16.MaxValue, instance.field_Int16); 694 Throws (field, instance, UInt16Enum.MaxValue); 695 Throws (field, instance, Int32Enum.MaxValue); 696 Throws (field, instance, UInt32Enum.MaxValue); 697 Throws (field, instance, Int64Enum.MaxValue); 698 Throws (field, instance, UInt64Enum.MaxValue); 699 field = fh.GetField ("field_UInt16"); 700 field.SetValue (instance, Byte.MaxValue); 701 Assert.AreEqual (Byte.MaxValue, instance.field_UInt16); 702 Throws (field, instance, SByte.MaxValue); 703 Throws (field, instance, true); 704 field.SetValue (instance, Char.MaxValue); 705 Assert.AreEqual (Char.MaxValue, instance.field_UInt16); 706 Throws (field, instance, Int16.MaxValue); 707 field.SetValue (instance, UInt16.MaxValue); 708 Assert.AreEqual (UInt16.MaxValue, instance.field_UInt16); 709 Throws (field, instance, Int32.MaxValue); 710 Throws (field, instance, UInt32.MaxValue); 711 Throws (field, instance, Int64.MaxValue); 712 Throws (field, instance, UInt64.MaxValue); 713 Throws (field, instance, Single.MaxValue); 714 Throws (field, instance, Double.MaxValue); 715 Throws (field, instance, IntPtr.Zero); 716 Throws (field, instance, UIntPtr.Zero); 717 Throws (field, instance, Decimal.MaxValue); 718 Throws (field, instance, DateTime.MaxValue); 719 field.SetValue (instance, ByteEnum.MaxValue); 720 Assert.AreEqual (Byte.MaxValue, instance.field_UInt16); 721 Throws (field, instance, SByteEnum.MaxValue); 722 Throws (field, instance, Int16Enum.MaxValue); 723 field.SetValue (instance, UInt16Enum.MaxValue); 724 Assert.AreEqual (UInt16.MaxValue, instance.field_UInt16); 725 Throws (field, instance, Int32Enum.MaxValue); 726 Throws (field, instance, UInt32Enum.MaxValue); 727 Throws (field, instance, Int64Enum.MaxValue); 728 Throws (field, instance, UInt64Enum.MaxValue); 729 field = fh.GetField ("field_Int32"); 730 field.SetValue (instance, Byte.MaxValue); 731 Assert.AreEqual (Byte.MaxValue, instance.field_Int32); 732 field.SetValue (instance, SByte.MaxValue); 733 Assert.AreEqual (SByte.MaxValue, instance.field_Int32); 734 Throws (field, instance, true); 735 field.SetValue (instance, Char.MaxValue); 736 Assert.AreEqual (Char.MaxValue, instance.field_Int32); 737 field.SetValue (instance, Int16.MaxValue); 738 Assert.AreEqual (Int16.MaxValue, instance.field_Int32); 739 field.SetValue (instance, UInt16.MaxValue); 740 Assert.AreEqual (UInt16.MaxValue, instance.field_Int32); 741 field.SetValue (instance, Int32.MaxValue); 742 Assert.AreEqual (Int32.MaxValue, instance.field_Int32); 743 Throws (field, instance, UInt32.MaxValue); 744 Throws (field, instance, Int64.MaxValue); 745 Throws (field, instance, UInt64.MaxValue); 746 Throws (field, instance, Single.MaxValue); 747 Throws (field, instance, Double.MaxValue); 748 Throws (field, instance, IntPtr.Zero); 749 Throws (field, instance, UIntPtr.Zero); 750 Throws (field, instance, Decimal.MaxValue); 751 Throws (field, instance, DateTime.MaxValue); 752 field.SetValue (instance, ByteEnum.MaxValue); 753 Assert.AreEqual (Byte.MaxValue, instance.field_Int32); 754 field.SetValue (instance, SByteEnum.MaxValue); 755 Assert.AreEqual (SByte.MaxValue, instance.field_Int32); 756 field.SetValue (instance, Int16Enum.MaxValue); 757 Assert.AreEqual (Int16.MaxValue, instance.field_Int32); 758 field.SetValue (instance, UInt16Enum.MaxValue); 759 Assert.AreEqual (UInt16.MaxValue, instance.field_Int32); 760 field.SetValue (instance, Int32Enum.MaxValue); 761 Assert.AreEqual (Int32.MaxValue, instance.field_Int32); 762 Throws (field, instance, UInt32Enum.MaxValue); 763 Throws (field, instance, Int64Enum.MaxValue); 764 Throws (field, instance, UInt64Enum.MaxValue); 765 field = fh.GetField ("field_UInt32"); 766 field.SetValue (instance, Byte.MaxValue); 767 Assert.AreEqual (Byte.MaxValue, instance.field_UInt32); 768 Throws (field, instance, SByte.MaxValue); 769 Throws (field, instance, true); 770 field.SetValue (instance, Char.MaxValue); 771 Assert.AreEqual (Char.MaxValue, instance.field_UInt32); 772 Throws (field, instance, Int16.MaxValue); 773 field.SetValue (instance, UInt16.MaxValue); 774 Assert.AreEqual (UInt16.MaxValue, instance.field_UInt32); 775 Throws (field, instance, Int32.MaxValue); 776 field.SetValue (instance, UInt32.MaxValue); 777 Assert.AreEqual (UInt32.MaxValue, instance.field_UInt32); 778 Throws (field, instance, Int64.MaxValue); 779 Throws (field, instance, UInt64.MaxValue); 780 Throws (field, instance, Single.MaxValue); 781 Throws (field, instance, Double.MaxValue); 782 Throws (field, instance, IntPtr.Zero); 783 Throws (field, instance, UIntPtr.Zero); 784 Throws (field, instance, Decimal.MaxValue); 785 Throws (field, instance, DateTime.MaxValue); 786 field.SetValue (instance, ByteEnum.MaxValue); 787 Assert.AreEqual (Byte.MaxValue, instance.field_UInt32); 788 Throws (field, instance, SByteEnum.MaxValue); 789 Throws (field, instance, Int16Enum.MaxValue); 790 field.SetValue (instance, UInt16Enum.MaxValue); 791 Assert.AreEqual (UInt16.MaxValue, instance.field_UInt32); 792 Throws (field, instance, Int32Enum.MaxValue); 793 field.SetValue (instance, UInt32Enum.MaxValue); 794 Assert.AreEqual (UInt32.MaxValue, instance.field_UInt32); 795 Throws (field, instance, Int64Enum.MaxValue); 796 Throws (field, instance, UInt64Enum.MaxValue); 797 field = fh.GetField ("field_Int64"); 798 field.SetValue (instance, Byte.MaxValue); 799 Assert.AreEqual (Byte.MaxValue, instance.field_Int64); 800 field.SetValue (instance, SByte.MaxValue); 801 Assert.AreEqual (SByte.MaxValue, instance.field_Int64); 802 Throws (field, instance, true); 803 field.SetValue (instance, Char.MaxValue); 804 Assert.AreEqual (Char.MaxValue, instance.field_Int64); 805 field.SetValue (instance, Int16.MaxValue); 806 Assert.AreEqual (Int16.MaxValue, instance.field_Int64); 807 field.SetValue (instance, UInt16.MaxValue); 808 Assert.AreEqual (UInt16.MaxValue, instance.field_Int64); 809 field.SetValue (instance, Int32.MaxValue); 810 Assert.AreEqual (Int32.MaxValue, instance.field_Int64); 811 field.SetValue (instance, UInt32.MaxValue); 812 Assert.AreEqual (UInt32.MaxValue, instance.field_Int64); 813 field.SetValue (instance, Int64.MaxValue); 814 Assert.AreEqual (Int64.MaxValue, instance.field_Int64); 815 Throws (field, instance, UInt64.MaxValue); 816 Throws (field, instance, Single.MaxValue); 817 Throws (field, instance, Double.MaxValue); 818 Throws (field, instance, IntPtr.Zero); 819 Throws (field, instance, UIntPtr.Zero); 820 Throws (field, instance, Decimal.MaxValue); 821 Throws (field, instance, DateTime.MaxValue); 822 field.SetValue (instance, ByteEnum.MaxValue); 823 Assert.AreEqual (Byte.MaxValue, instance.field_Int64); 824 field.SetValue (instance, SByteEnum.MaxValue); 825 Assert.AreEqual (SByte.MaxValue, instance.field_Int64); 826 field.SetValue (instance, Int16Enum.MaxValue); 827 Assert.AreEqual (Int16.MaxValue, instance.field_Int64); 828 field.SetValue (instance, UInt16Enum.MaxValue); 829 Assert.AreEqual (UInt16.MaxValue, instance.field_Int64); 830 field.SetValue (instance, Int32Enum.MaxValue); 831 Assert.AreEqual (Int32.MaxValue, instance.field_Int64); 832 field.SetValue (instance, UInt32Enum.MaxValue); 833 Assert.AreEqual (UInt32.MaxValue, instance.field_Int64); 834 field.SetValue (instance, Int64Enum.MaxValue); 835 Assert.AreEqual (Int64.MaxValue, instance.field_Int64); 836 Throws (field, instance, UInt64Enum.MaxValue); 837 field = fh.GetField ("field_UInt64"); 838 field.SetValue (instance, Byte.MaxValue); 839 Assert.AreEqual (Byte.MaxValue, instance.field_UInt64); 840 Throws (field, instance, SByte.MaxValue); 841 Throws (field, instance, true); 842 field.SetValue (instance, Char.MaxValue); 843 Assert.AreEqual (Char.MaxValue, instance.field_UInt64); 844 Throws (field, instance, Int16.MaxValue); 845 field.SetValue (instance, UInt16.MaxValue); 846 Assert.AreEqual (UInt16.MaxValue, instance.field_UInt64); 847 Throws (field, instance, Int32.MaxValue); 848 field.SetValue (instance, UInt32.MaxValue); 849 Assert.AreEqual (UInt32.MaxValue, instance.field_UInt64); 850 Throws (field, instance, Int64.MaxValue); 851 field.SetValue (instance, UInt64.MaxValue); 852 Assert.AreEqual (UInt64.MaxValue, instance.field_UInt64); 853 Throws (field, instance, Single.MaxValue); 854 Throws (field, instance, Double.MaxValue); 855 Throws (field, instance, IntPtr.Zero); 856 Throws (field, instance, UIntPtr.Zero); 857 Throws (field, instance, Decimal.MaxValue); 858 Throws (field, instance, DateTime.MaxValue); 859 field.SetValue (instance, ByteEnum.MaxValue); 860 Assert.AreEqual (Byte.MaxValue, instance.field_UInt64); 861 Throws (field, instance, SByteEnum.MaxValue); 862 Throws (field, instance, Int16Enum.MaxValue); 863 field.SetValue (instance, UInt16Enum.MaxValue); 864 Assert.AreEqual (UInt16.MaxValue, instance.field_UInt64); 865 Throws (field, instance, Int32Enum.MaxValue); 866 field.SetValue (instance, UInt32Enum.MaxValue); 867 Assert.AreEqual (UInt32.MaxValue, instance.field_UInt64); 868 Throws (field, instance, Int64Enum.MaxValue); 869 field.SetValue (instance, UInt64Enum.MaxValue); 870 Assert.AreEqual (UInt64.MaxValue, instance.field_UInt64); 871 field = fh.GetField ("field_Single"); 872 field.SetValue (instance, Byte.MaxValue); 873 Assert.AreEqual (Byte.MaxValue, instance.field_Single); 874 field.SetValue (instance, SByte.MaxValue); 875 Assert.AreEqual (SByte.MaxValue, instance.field_Single); 876 Throws (field, instance, true); 877 field.SetValue (instance, Char.MaxValue); 878 Assert.AreEqual ((Single) Char.MaxValue, instance.field_Single); 879 field.SetValue (instance, Int16.MaxValue); 880 Assert.AreEqual (Int16.MaxValue, instance.field_Single); 881 field.SetValue (instance, UInt16.MaxValue); 882 Assert.AreEqual (UInt16.MaxValue, instance.field_Single); 883 field.SetValue (instance, Int32.MaxValue); 884 Assert.AreEqual ((Single)Int32.MaxValue, instance.field_Single); 885 field.SetValue (instance, UInt32.MaxValue); 886 Assert.AreEqual ((Single) UInt32.MaxValue, instance.field_Single); 887 field.SetValue (instance, Int64.MaxValue); 888 Assert.AreEqual (Int64.MaxValue, instance.field_Single); 889 field.SetValue (instance, UInt64.MaxValue); 890 Assert.AreEqual (UInt64.MaxValue, instance.field_Single); 891 field.SetValue (instance, Single.MaxValue); 892 Assert.AreEqual (Single.MaxValue, instance.field_Single); 893 Throws (field, instance, Double.MaxValue); 894 Throws (field, instance, IntPtr.Zero); 895 Throws (field, instance, UIntPtr.Zero); 896 Throws (field, instance, Decimal.MaxValue); 897 Throws (field, instance, DateTime.MaxValue); 898 field.SetValue (instance, ByteEnum.MaxValue); 899 Assert.AreEqual (Byte.MaxValue, instance.field_Single); 900 field.SetValue (instance, SByteEnum.MaxValue); 901 Assert.AreEqual (SByte.MaxValue, instance.field_Single); 902 field.SetValue (instance, Int16Enum.MaxValue); 903 Assert.AreEqual (Int16.MaxValue, instance.field_Single); 904 field.SetValue (instance, UInt16Enum.MaxValue); 905 Assert.AreEqual (UInt16.MaxValue, instance.field_Single); 906 field.SetValue (instance, Int32Enum.MaxValue); 907 Assert.AreEqual ((Single) Int32.MaxValue, instance.field_Single); 908 field.SetValue (instance, UInt32Enum.MaxValue); 909 Assert.AreEqual ((Single) UInt32.MaxValue, instance.field_Single); 910 field.SetValue (instance, Int64Enum.MaxValue); 911 Assert.AreEqual (Int64.MaxValue, instance.field_Single); 912 field.SetValue (instance, UInt64Enum.MaxValue); 913 Assert.AreEqual (UInt64.MaxValue, instance.field_Single); 914 field = fh.GetField ("field_Double"); 915 field.SetValue (instance, Byte.MaxValue); 916 Assert.AreEqual (Byte.MaxValue, instance.field_Double); 917 field.SetValue (instance, SByte.MaxValue); 918 Assert.AreEqual (SByte.MaxValue, instance.field_Double); 919 Throws (field, instance, true); 920 field.SetValue (instance, Char.MaxValue); 921 Assert.AreEqual ((Double) Char.MaxValue, instance.field_Double); 922 field.SetValue (instance, Int16.MaxValue); 923 Assert.AreEqual (Int16.MaxValue, instance.field_Double); 924 field.SetValue (instance, UInt16.MaxValue); 925 Assert.AreEqual (UInt16.MaxValue, instance.field_Double); 926 field.SetValue (instance, Int32.MaxValue); 927 Assert.AreEqual (Int32.MaxValue, instance.field_Double); 928 field.SetValue (instance, UInt32.MaxValue); 929 Assert.AreEqual (UInt32.MaxValue, instance.field_Double); 930 field.SetValue (instance, Int64.MaxValue); 931 Assert.AreEqual (Int64.MaxValue, instance.field_Double); 932 field.SetValue (instance, UInt64.MaxValue); 933 Assert.AreEqual (UInt64.MaxValue, instance.field_Double); 934 field.SetValue (instance, Single.MaxValue); 935 Assert.AreEqual (Single.MaxValue, instance.field_Double); 936 field.SetValue (instance, Double.MaxValue); 937 Assert.AreEqual (Double.MaxValue, instance.field_Double); 938 Throws (field, instance, IntPtr.Zero); 939 Throws (field, instance, UIntPtr.Zero); 940 Throws (field, instance, Decimal.MaxValue); 941 Throws (field, instance, DateTime.MaxValue); 942 field.SetValue (instance, ByteEnum.MaxValue); 943 Assert.AreEqual (Byte.MaxValue, instance.field_Double); 944 field.SetValue (instance, SByteEnum.MaxValue); 945 Assert.AreEqual (SByte.MaxValue, instance.field_Double); 946 field.SetValue (instance, Int16Enum.MaxValue); 947 Assert.AreEqual (Int16.MaxValue, instance.field_Double); 948 field.SetValue (instance, UInt16Enum.MaxValue); 949 Assert.AreEqual (UInt16.MaxValue, instance.field_Double); 950 field.SetValue (instance, Int32Enum.MaxValue); 951 Assert.AreEqual (Int32.MaxValue, instance.field_Double); 952 field.SetValue (instance, UInt32Enum.MaxValue); 953 Assert.AreEqual (UInt32.MaxValue, instance.field_Double); 954 field.SetValue (instance, Int64Enum.MaxValue); 955 Assert.AreEqual (Int64.MaxValue, instance.field_Double); 956 field.SetValue (instance, UInt64Enum.MaxValue); 957 Assert.AreEqual (UInt64.MaxValue, instance.field_Double); 958 field = fh.GetField ("field_IntPtr"); 959 Throws (field, instance, Byte.MaxValue); 960 Throws (field, instance, SByte.MaxValue); 961 Throws (field, instance, true); 962 Throws (field, instance, Char.MaxValue); 963 Throws (field, instance, Int16.MaxValue); 964 Throws (field, instance, UInt16.MaxValue); 965 Throws (field, instance, Int32.MaxValue); 966 Throws (field, instance, UInt32.MaxValue); 967 Throws (field, instance, Int64.MaxValue); 968 Throws (field, instance, UInt64.MaxValue); 969 Throws (field, instance, Single.MaxValue); 970 Throws (field, instance, Double.MaxValue); 971 field.SetValue (instance, IntPtr.Zero); 972 Assert.AreEqual (IntPtr.Zero, instance.field_IntPtr); 973 Throws (field, instance, UIntPtr.Zero); 974 Throws (field, instance, Decimal.MaxValue); 975 Throws (field, instance, DateTime.MaxValue); 976 Throws (field, instance, ByteEnum.MaxValue); 977 Throws (field, instance, SByteEnum.MaxValue); 978 Throws (field, instance, Int16Enum.MaxValue); 979 Throws (field, instance, UInt16Enum.MaxValue); 980 Throws (field, instance, Int32Enum.MaxValue); 981 Throws (field, instance, UInt32Enum.MaxValue); 982 Throws (field, instance, Int64Enum.MaxValue); 983 Throws (field, instance, UInt64Enum.MaxValue); 984 field = fh.GetField ("field_UIntPtr"); 985 Throws (field, instance, Byte.MaxValue); 986 Throws (field, instance, SByte.MaxValue); 987 Throws (field, instance, true); 988 Throws (field, instance, Char.MaxValue); 989 Throws (field, instance, Int16.MaxValue); 990 Throws (field, instance, UInt16.MaxValue); 991 Throws (field, instance, Int32.MaxValue); 992 Throws (field, instance, UInt32.MaxValue); 993 Throws (field, instance, Int64.MaxValue); 994 Throws (field, instance, UInt64.MaxValue); 995 Throws (field, instance, Single.MaxValue); 996 Throws (field, instance, Double.MaxValue); 997 Throws (field, instance, IntPtr.Zero); 998 field.SetValue (instance, UIntPtr.Zero); 999 Assert.AreEqual (UIntPtr.Zero, instance.field_UIntPtr); 1000 Throws (field, instance, Decimal.MaxValue); 1001 Throws (field, instance, DateTime.MaxValue); 1002 Throws (field, instance, ByteEnum.MaxValue); 1003 Throws (field, instance, SByteEnum.MaxValue); 1004 Throws (field, instance, Int16Enum.MaxValue); 1005 Throws (field, instance, UInt16Enum.MaxValue); 1006 Throws (field, instance, Int32Enum.MaxValue); 1007 Throws (field, instance, UInt32Enum.MaxValue); 1008 Throws (field, instance, Int64Enum.MaxValue); 1009 Throws (field, instance, UInt64Enum.MaxValue); 1010 field = fh.GetField ("field_Decimal"); 1011 Throws (field, instance, Byte.MaxValue); 1012 Throws (field, instance, SByte.MaxValue); 1013 Throws (field, instance, true); 1014 Throws (field, instance, Char.MaxValue); 1015 Throws (field, instance, Int16.MaxValue); 1016 Throws (field, instance, UInt16.MaxValue); 1017 Throws (field, instance, Int32.MaxValue); 1018 Throws (field, instance, UInt32.MaxValue); 1019 Throws (field, instance, Int64.MaxValue); 1020 Throws (field, instance, UInt64.MaxValue); 1021 Throws (field, instance, Single.MaxValue); 1022 Throws (field, instance, Double.MaxValue); 1023 Throws (field, instance, IntPtr.Zero); 1024 Throws (field, instance, UIntPtr.Zero); 1025 field.SetValue (instance, Decimal.MaxValue); 1026 Assert.AreEqual (Decimal.MaxValue, instance.field_Decimal); 1027 Throws (field, instance, DateTime.MaxValue); 1028 Throws (field, instance, ByteEnum.MaxValue); 1029 Throws (field, instance, SByteEnum.MaxValue); 1030 Throws (field, instance, Int16Enum.MaxValue); 1031 Throws (field, instance, UInt16Enum.MaxValue); 1032 Throws (field, instance, Int32Enum.MaxValue); 1033 Throws (field, instance, UInt32Enum.MaxValue); 1034 Throws (field, instance, Int64Enum.MaxValue); 1035 Throws (field, instance, UInt64Enum.MaxValue); 1036 field = fh.GetField ("field_DateTime"); 1037 Throws (field, instance, Byte.MaxValue); 1038 Throws (field, instance, SByte.MaxValue); 1039 Throws (field, instance, true); 1040 Throws (field, instance, Char.MaxValue); 1041 Throws (field, instance, Int16.MaxValue); 1042 Throws (field, instance, UInt16.MaxValue); 1043 Throws (field, instance, Int32.MaxValue); 1044 Throws (field, instance, UInt32.MaxValue); 1045 Throws (field, instance, Int64.MaxValue); 1046 Throws (field, instance, UInt64.MaxValue); 1047 Throws (field, instance, Single.MaxValue); 1048 Throws (field, instance, Double.MaxValue); 1049 Throws (field, instance, IntPtr.Zero); 1050 Throws (field, instance, UIntPtr.Zero); 1051 Throws (field, instance, Decimal.MaxValue); 1052 field.SetValue (instance, DateTime.MaxValue); 1053 Assert.AreEqual (DateTime.MaxValue, instance.field_DateTime); 1054 Throws (field, instance, ByteEnum.MaxValue); 1055 Throws (field, instance, SByteEnum.MaxValue); 1056 Throws (field, instance, Int16Enum.MaxValue); 1057 Throws (field, instance, UInt16Enum.MaxValue); 1058 Throws (field, instance, Int32Enum.MaxValue); 1059 Throws (field, instance, UInt32Enum.MaxValue); 1060 Throws (field, instance, Int64Enum.MaxValue); 1061 Throws (field, instance, UInt64Enum.MaxValue); 1062 field = fh.GetField ("field_ByteEnum"); 1063 field.SetValue (instance, Byte.MaxValue); 1064 Assert.AreEqual (ByteEnum.MaxValue, instance.field_ByteEnum); 1065 Throws (field, instance, SByte.MaxValue); 1066 Throws (field, instance, true); 1067 Throws (field, instance, Char.MaxValue); 1068 Throws (field, instance, Int16.MaxValue); 1069 Throws (field, instance, UInt16.MaxValue); 1070 Throws (field, instance, Int32.MaxValue); 1071 Throws (field, instance, UInt32.MaxValue); 1072 Throws (field, instance, Int64.MaxValue); 1073 Throws (field, instance, UInt64.MaxValue); 1074 Throws (field, instance, Single.MaxValue); 1075 Throws (field, instance, Double.MaxValue); 1076 Throws (field, instance, IntPtr.Zero); 1077 Throws (field, instance, UIntPtr.Zero); 1078 Throws (field, instance, Decimal.MaxValue); 1079 Throws (field, instance, DateTime.MaxValue); 1080 field.SetValue (instance, ByteEnum.MaxValue); 1081 Assert.AreEqual (ByteEnum.MaxValue, instance.field_ByteEnum); 1082 Throws (field, instance, SByteEnum.MaxValue); 1083 Throws (field, instance, Int16Enum.MaxValue); 1084 Throws (field, instance, UInt16Enum.MaxValue); 1085 Throws (field, instance, Int32Enum.MaxValue); 1086 Throws (field, instance, UInt32Enum.MaxValue); 1087 Throws (field, instance, Int64Enum.MaxValue); 1088 Throws (field, instance, UInt64Enum.MaxValue); 1089 field = fh.GetField ("field_SByteEnum"); 1090 Throws (field, instance, Byte.MaxValue); 1091 field.SetValue (instance, SByte.MaxValue); 1092 Assert.AreEqual (SByteEnum.MaxValue, instance.field_SByteEnum); 1093 Throws (field, instance, true); 1094 Throws (field, instance, Char.MaxValue); 1095 Throws (field, instance, Int16.MaxValue); 1096 Throws (field, instance, UInt16.MaxValue); 1097 Throws (field, instance, Int32.MaxValue); 1098 Throws (field, instance, UInt32.MaxValue); 1099 Throws (field, instance, Int64.MaxValue); 1100 Throws (field, instance, UInt64.MaxValue); 1101 Throws (field, instance, Single.MaxValue); 1102 Throws (field, instance, Double.MaxValue); 1103 Throws (field, instance, IntPtr.Zero); 1104 Throws (field, instance, UIntPtr.Zero); 1105 Throws (field, instance, Decimal.MaxValue); 1106 Throws (field, instance, DateTime.MaxValue); 1107 Throws (field, instance, ByteEnum.MaxValue); 1108 field.SetValue (instance, SByteEnum.MaxValue); 1109 Assert.AreEqual (SByteEnum.MaxValue, instance.field_SByteEnum); 1110 Throws (field, instance, Int16Enum.MaxValue); 1111 Throws (field, instance, UInt16Enum.MaxValue); 1112 Throws (field, instance, Int32Enum.MaxValue); 1113 Throws (field, instance, UInt32Enum.MaxValue); 1114 Throws (field, instance, Int64Enum.MaxValue); 1115 Throws (field, instance, UInt64Enum.MaxValue); 1116 field = fh.GetField ("field_Int16Enum"); 1117 field.SetValue (instance, Byte.MaxValue); 1118 Assert.AreEqual (Byte.MaxValue, (byte) instance.field_Int16Enum); 1119 field.SetValue (instance, SByte.MaxValue); 1120 Assert.AreEqual (SByte.MaxValue, (sbyte) instance.field_Int16Enum); 1121 Throws (field, instance, true); 1122 Throws (field, instance, Char.MaxValue); 1123 field.SetValue (instance, Int16.MaxValue); 1124 Assert.AreEqual (Int16Enum.MaxValue, instance.field_Int16Enum); 1125 Throws (field, instance, UInt16.MaxValue); 1126 Throws (field, instance, Int32.MaxValue); 1127 Throws (field, instance, UInt32.MaxValue); 1128 Throws (field, instance, Int64.MaxValue); 1129 Throws (field, instance, UInt64.MaxValue); 1130 Throws (field, instance, Single.MaxValue); 1131 Throws (field, instance, Double.MaxValue); 1132 Throws (field, instance, IntPtr.Zero); 1133 Throws (field, instance, UIntPtr.Zero); 1134 Throws (field, instance, Decimal.MaxValue); 1135 Throws (field, instance, DateTime.MaxValue); 1136 field.SetValue (instance, ByteEnum.MaxValue); 1137 Assert.AreEqual (ByteEnum.MaxValue, (ByteEnum) instance.field_Int16Enum); 1138 field.SetValue (instance, SByteEnum.MaxValue); 1139 Assert.AreEqual (SByteEnum.MaxValue, (SByteEnum) instance.field_Int16Enum); 1140 field.SetValue (instance, Int16Enum.MaxValue); 1141 Assert.AreEqual (Int16Enum.MaxValue, instance.field_Int16Enum); 1142 Throws (field, instance, UInt16Enum.MaxValue); 1143 Throws (field, instance, Int32Enum.MaxValue); 1144 Throws (field, instance, UInt32Enum.MaxValue); 1145 Throws (field, instance, Int64Enum.MaxValue); 1146 Throws (field, instance, UInt64Enum.MaxValue); 1147 field = fh.GetField ("field_UInt16Enum"); 1148 field.SetValue (instance, Byte.MaxValue); 1149 Assert.AreEqual (Byte.MaxValue, (byte) instance.field_UInt16Enum); 1150 Throws (field, instance, SByte.MaxValue); 1151 Throws (field, instance, true); 1152 field.SetValue (instance, Char.MaxValue); 1153 Assert.AreEqual (Char.MaxValue, (char) instance.field_UInt16Enum); 1154 Throws (field, instance, Int16.MaxValue); 1155 field.SetValue (instance, UInt16.MaxValue); 1156 Assert.AreEqual (UInt16.MaxValue, (UInt16) instance.field_UInt16Enum); 1157 Throws (field, instance, Int32.MaxValue); 1158 Throws (field, instance, UInt32.MaxValue); 1159 Throws (field, instance, Int64.MaxValue); 1160 Throws (field, instance, UInt64.MaxValue); 1161 Throws (field, instance, Single.MaxValue); 1162 Throws (field, instance, Double.MaxValue); 1163 Throws (field, instance, IntPtr.Zero); 1164 Throws (field, instance, UIntPtr.Zero); 1165 Throws (field, instance, Decimal.MaxValue); 1166 Throws (field, instance, DateTime.MaxValue); 1167 field.SetValue (instance, ByteEnum.MaxValue); 1168 Assert.AreEqual (ByteEnum.MaxValue, (ByteEnum) instance.field_UInt16Enum); 1169 Throws (field, instance, SByteEnum.MaxValue); 1170 Throws (field, instance, Int16Enum.MaxValue); 1171 field.SetValue (instance, UInt16Enum.MaxValue); 1172 Assert.AreEqual (UInt16Enum.MaxValue, instance.field_UInt16Enum); 1173 Throws (field, instance, Int32Enum.MaxValue); 1174 Throws (field, instance, UInt32Enum.MaxValue); 1175 Throws (field, instance, Int64Enum.MaxValue); 1176 Throws (field, instance, UInt64Enum.MaxValue); 1177 field = fh.GetField ("field_Int32Enum"); 1178 field.SetValue (instance, Byte.MaxValue); 1179 Assert.AreEqual (Byte.MaxValue, (byte) instance.field_Int32Enum); 1180 field.SetValue (instance, SByte.MaxValue); 1181 Assert.AreEqual (SByte.MaxValue, (sbyte) instance.field_Int32Enum); 1182 Throws (field, instance, true); 1183 field.SetValue (instance, Char.MaxValue); 1184 Assert.AreEqual (Char.MaxValue, (char) instance.field_Int32Enum); 1185 field.SetValue (instance, Int16.MaxValue); 1186 Assert.AreEqual (Int16.MaxValue, (Int16) instance.field_Int32Enum); 1187 field.SetValue (instance, UInt16.MaxValue); 1188 Assert.AreEqual (UInt16.MaxValue, (UInt16) instance.field_Int32Enum); 1189 field.SetValue (instance, Int32.MaxValue); 1190 Assert.AreEqual (Int32.MaxValue, (Int32) instance.field_Int32Enum); 1191 Throws (field, instance, UInt32.MaxValue); 1192 Throws (field, instance, Int64.MaxValue); 1193 Throws (field, instance, UInt64.MaxValue); 1194 Throws (field, instance, Single.MaxValue); 1195 Throws (field, instance, Double.MaxValue); 1196 Throws (field, instance, IntPtr.Zero); 1197 Throws (field, instance, UIntPtr.Zero); 1198 Throws (field, instance, Decimal.MaxValue); 1199 Throws (field, instance, DateTime.MaxValue); 1200 field.SetValue (instance, ByteEnum.MaxValue); 1201 Assert.AreEqual (ByteEnum.MaxValue, (ByteEnum) instance.field_Int32Enum); 1202 field.SetValue (instance, SByteEnum.MaxValue); 1203 Assert.AreEqual (SByteEnum.MaxValue, (SByteEnum) instance.field_Int32Enum); 1204 field.SetValue (instance, Int16Enum.MaxValue); 1205 Assert.AreEqual (Int16Enum.MaxValue, (Int16Enum) instance.field_Int32Enum); 1206 field.SetValue (instance, UInt16Enum.MaxValue); 1207 Assert.AreEqual (UInt16Enum.MaxValue, (UInt16Enum) instance.field_Int32Enum); 1208 field.SetValue (instance, Int32Enum.MaxValue); 1209 Assert.AreEqual (Int32Enum.MaxValue, instance.field_Int32Enum); 1210 Throws (field, instance, UInt32Enum.MaxValue); 1211 Throws (field, instance, Int64Enum.MaxValue); 1212 Throws (field, instance, UInt64Enum.MaxValue); 1213 field = fh.GetField ("field_UInt32Enum"); 1214 field.SetValue (instance, Byte.MaxValue); 1215 Assert.AreEqual (Byte.MaxValue, (byte) instance.field_UInt32Enum); 1216 Throws (field, instance, SByte.MaxValue); 1217 Throws (field, instance, true); 1218 field.SetValue (instance, Char.MaxValue); 1219 Assert.AreEqual (Char.MaxValue, (char) instance.field_UInt32Enum); 1220 Throws (field, instance, Int16.MaxValue); 1221 field.SetValue (instance, UInt16.MaxValue); 1222 Assert.AreEqual (UInt16.MaxValue, (UInt16) instance.field_UInt32Enum); 1223 Throws (field, instance, Int32.MaxValue); 1224 field.SetValue (instance, UInt32.MaxValue); 1225 Assert.AreEqual (UInt32.MaxValue, (UInt32) instance.field_UInt32Enum); 1226 Throws (field, instance, Int64.MaxValue); 1227 Throws (field, instance, UInt64.MaxValue); 1228 Throws (field, instance, Single.MaxValue); 1229 Throws (field, instance, Double.MaxValue); 1230 Throws (field, instance, IntPtr.Zero); 1231 Throws (field, instance, UIntPtr.Zero); 1232 Throws (field, instance, Decimal.MaxValue); 1233 Throws (field, instance, DateTime.MaxValue); 1234 field.SetValue (instance, ByteEnum.MaxValue); 1235 Assert.AreEqual (ByteEnum.MaxValue, (ByteEnum) instance.field_UInt32Enum); 1236 Throws (field, instance, SByteEnum.MaxValue); 1237 Throws (field, instance, Int16Enum.MaxValue); 1238 field.SetValue (instance, UInt16Enum.MaxValue); 1239 Assert.AreEqual (UInt16Enum.MaxValue, (UInt16Enum) instance.field_UInt32Enum); 1240 Throws (field, instance, Int32Enum.MaxValue); 1241 field.SetValue (instance, UInt32Enum.MaxValue); 1242 Assert.AreEqual (UInt32Enum.MaxValue, instance.field_UInt32Enum); 1243 Throws (field, instance, Int64Enum.MaxValue); 1244 Throws (field, instance, UInt64Enum.MaxValue); 1245 field = fh.GetField ("field_Int64Enum"); 1246 field.SetValue (instance, Byte.MaxValue); 1247 Assert.AreEqual (Byte.MaxValue, (byte) instance.field_Int64Enum); 1248 field.SetValue (instance, SByte.MaxValue); 1249 Assert.AreEqual (SByte.MaxValue, (sbyte) instance.field_Int64Enum); 1250 Throws (field, instance, true); 1251 field.SetValue (instance, Char.MaxValue); 1252 Assert.AreEqual (Char.MaxValue, (char) instance.field_Int64Enum); 1253 field.SetValue (instance, Int16.MaxValue); 1254 Assert.AreEqual (Int16.MaxValue, (Int16) instance.field_Int64Enum); 1255 field.SetValue (instance, UInt16.MaxValue); 1256 Assert.AreEqual (UInt16.MaxValue, (UInt16) instance.field_Int64Enum); 1257 field.SetValue (instance, Int32.MaxValue); 1258 Assert.AreEqual (Int32.MaxValue, (Int32) instance.field_Int64Enum); 1259 field.SetValue (instance, UInt32.MaxValue); 1260 Assert.AreEqual (UInt32.MaxValue, (UInt32) instance.field_Int64Enum); 1261 field.SetValue (instance, Int64.MaxValue); 1262 Assert.AreEqual (Int64.MaxValue, (Int64) instance.field_Int64Enum); 1263 Throws (field, instance, UInt64.MaxValue); 1264 Throws (field, instance, Single.MaxValue); 1265 Throws (field, instance, Double.MaxValue); 1266 Throws (field, instance, IntPtr.Zero); 1267 Throws (field, instance, UIntPtr.Zero); 1268 Throws (field, instance, Decimal.MaxValue); 1269 Throws (field, instance, DateTime.MaxValue); 1270 field.SetValue (instance, ByteEnum.MaxValue); 1271 Assert.AreEqual (ByteEnum.MaxValue, (ByteEnum) instance.field_Int64Enum); 1272 field.SetValue (instance, SByteEnum.MaxValue); 1273 Assert.AreEqual (SByteEnum.MaxValue, (SByteEnum) instance.field_Int64Enum); 1274 field.SetValue (instance, Int16Enum.MaxValue); 1275 Assert.AreEqual (Int16Enum.MaxValue, (Int16Enum) instance.field_Int64Enum); 1276 field.SetValue (instance, UInt16Enum.MaxValue); 1277 Assert.AreEqual (UInt16Enum.MaxValue, (UInt16Enum) instance.field_Int64Enum); 1278 field.SetValue (instance, Int32Enum.MaxValue); 1279 Assert.AreEqual (Int32Enum.MaxValue, (Int32Enum) instance.field_Int64Enum); 1280 field.SetValue (instance, UInt32Enum.MaxValue); 1281 Assert.AreEqual (UInt32Enum.MaxValue, (UInt32Enum) instance.field_Int64Enum); 1282 field.SetValue (instance, Int64Enum.MaxValue); 1283 Assert.AreEqual (Int64Enum.MaxValue, instance.field_Int64Enum); 1284 Throws (field, instance, UInt64Enum.MaxValue); 1285 field = fh.GetField ("field_UInt64Enum"); 1286 field.SetValue (instance, Byte.MaxValue); 1287 Assert.AreEqual (Byte.MaxValue, (byte) instance.field_UInt64Enum); 1288 Throws (field, instance, SByte.MaxValue); 1289 Throws (field, instance, true); 1290 field.SetValue (instance, Char.MaxValue); 1291 Assert.AreEqual (Char.MaxValue, (char) instance.field_UInt64Enum); 1292 Throws (field, instance, Int16.MaxValue); 1293 field.SetValue (instance, UInt16.MaxValue); 1294 Assert.AreEqual (UInt16.MaxValue, (UInt16) instance.field_UInt64Enum); 1295 Throws (field, instance, Int32.MaxValue); 1296 field.SetValue (instance, UInt32.MaxValue); 1297 Assert.AreEqual (UInt32.MaxValue, (UInt32) instance.field_UInt64Enum); 1298 Throws (field, instance, Int64.MaxValue); 1299 field.SetValue (instance, UInt64.MaxValue); 1300 Assert.AreEqual (UInt64.MaxValue, (UInt64) instance.field_UInt64Enum); 1301 Throws (field, instance, Single.MaxValue); 1302 Throws (field, instance, Double.MaxValue); 1303 Throws (field, instance, IntPtr.Zero); 1304 Throws (field, instance, UIntPtr.Zero); 1305 Throws (field, instance, Decimal.MaxValue); 1306 Throws (field, instance, DateTime.MaxValue); 1307 field.SetValue (instance, ByteEnum.MaxValue); 1308 Assert.AreEqual (ByteEnum.MaxValue, (ByteEnum) instance.field_UInt64Enum); 1309 Throws (field, instance, SByteEnum.MaxValue); 1310 Throws (field, instance, Int16Enum.MaxValue); 1311 field.SetValue (instance, UInt16Enum.MaxValue); 1312 Assert.AreEqual (UInt16Enum.MaxValue, (UInt16Enum) instance.field_UInt64Enum); 1313 Throws (field, instance, Int32Enum.MaxValue); 1314 field.SetValue (instance, UInt32Enum.MaxValue); 1315 Assert.AreEqual (UInt32Enum.MaxValue, (UInt32Enum) instance.field_UInt64Enum); 1316 Throws (field, instance, Int64Enum.MaxValue); 1317 field.SetValue (instance, UInt64Enum.MaxValue); 1318 Assert.AreEqual (UInt64Enum.MaxValue, instance.field_UInt64Enum); 1319 1320 } 1321 Throws(FieldInfo field, object instance, object value)1322 static void Throws (FieldInfo field, object instance, object value) 1323 { 1324 try { 1325 field.SetValue (instance, value); 1326 Assert.Fail ("ArgumentException expected"); 1327 } catch (ArgumentException ex) { 1328 } 1329 } 1330 1331 public object[] ObjectArrayField; 1332 1333 [Test] TestSetValueArray()1334 public void TestSetValueArray () 1335 { 1336 var field = typeof (FieldInfoTest).GetField ("ObjectArrayField"); 1337 var instance = new FieldInfoTest (); 1338 field.SetValue (instance, new string[] { "3" }); 1339 field.SetValue (instance, null); 1340 1341 Throws (field, instance, new int[] { 3 }); 1342 } 1343 1344 struct TestFields { 1345 public int MaxValue; 1346 public string str; 1347 } 1348 1349 [Test] SetValueDirect()1350 public void SetValueDirect () 1351 { 1352 TestFields fields = new TestFields { MaxValue = 1234, str = "A" }; 1353 1354 FieldInfo info = fields.GetType ().GetField ("MaxValue"); 1355 TypedReference reference = __makeref(fields); 1356 info.SetValueDirect (reference, 4096); 1357 Assert.AreEqual (4096, fields.MaxValue); 1358 1359 info = fields.GetType ().GetField ("str"); 1360 reference = __makeref(fields); 1361 info.SetValueDirect (reference, "B"); 1362 Assert.AreEqual ("B", fields.str); 1363 } 1364 1365 [Test] GetValueContextBoundObject()1366 public void GetValueContextBoundObject () 1367 { 1368 var instance = new CBOTest (); 1369 1370 var field1 = typeof (CBOTest).GetField ("d1"); 1371 var d1 = field1.GetValue (instance); 1372 Assert.AreEqual ((double)d1, 14.0, "d1"); 1373 1374 var field2 = typeof (CBOTest).GetField ("d2"); 1375 var d2 = field2.GetValue (instance); 1376 Assert.AreEqual ((double)d2, -20, "d2"); 1377 1378 var field3 = typeof (CBOTest).GetField ("s1"); 1379 var s1 = field3.GetValue (instance); 1380 Assert.AreEqual (s1, "abcd", "s1"); 1381 1382 var field4 = typeof (CBOTest).GetField ("s2"); 1383 var s2 = field4.GetValue (instance); 1384 Assert.AreEqual (s2, "hijkl", "s2"); 1385 } 1386 1387 [Test] SetValueContextBoundObject()1388 public void SetValueContextBoundObject () 1389 { 1390 var instance = new CBOTest (); 1391 1392 var field1 = typeof (CBOTest).GetField ("d1"); 1393 field1.SetValue (instance, 90.3); 1394 var d1 = field1.GetValue (instance); 1395 Assert.AreEqual ((double)d1, 90.3, "d1"); 1396 1397 var field2 = typeof (CBOTest).GetField ("d2"); 1398 field2.SetValue (instance, 1); 1399 var d2 = field2.GetValue (instance); 1400 Assert.AreEqual ((double)d2, 1, "d2"); 1401 1402 var field3 = typeof (CBOTest).GetField ("s1"); 1403 field3.SetValue (instance, "//////"); 1404 var s1 = field3.GetValue (instance); 1405 Assert.AreEqual (s1, "//////", "s1"); 1406 1407 var field4 = typeof (CBOTest).GetField ("s2"); 1408 field4.SetValue (instance, "This is a string"); 1409 var s2 = field4.GetValue (instance); 1410 Assert.AreEqual (s2, "This is a string", "s2"); 1411 1412 } 1413 1414 class CBOTest : ContextBoundObject { 1415 public double d1 = 14.0; 1416 public double d2 = -20.0; 1417 public string s1 = "abcd"; 1418 public string s2 = "hijkl"; 1419 } 1420 1421 1422 public IntEnum PPP; 1423 1424 public class Foo<T> 1425 { 1426 /* 1427 The whole point of this field is to make sure we don't create the vtable layout 1428 when loading the value of constants for Foo<>. See bug #594942. 1429 1430 */ 1431 public T dummy; 1432 public static int field; 1433 public const int constant = 10; 1434 public const string sconstant = "waa"; 1435 public const IntEnum econstant = IntEnum.Third; 1436 } 1437 1438 public enum IntEnum { 1439 First = 1, 1440 Second = 2, 1441 Third = 3 1442 } 1443 1444 public enum LongEnum : long { 1445 First = 1, 1446 Second = 2, 1447 Third = 3 1448 } 1449 1450 public const int int_field = 5; 1451 public const long long_field = Int64.MaxValue; 1452 public const IntEnum int_enum_field = IntEnum.Second; 1453 public const LongEnum long_enum_field = LongEnum.Second; 1454 public const string string_field = "Hello"; 1455 public const FieldInfoTest object_field = null; 1456 public int non_const_field; 1457 1458 } 1459 1460 // We do not refernece the field, that is expected 1461 #pragma warning disable 169 1462 // Helper classes 1463 class RefOnlyFieldClass 1464 { 1465 // Helper property 1466 static int RefOnlyField; 1467 } 1468 #pragma warning restore 169 1469 1470 class NonPublicFieldClass 1471 { 1472 protected int protectedField; 1473 Dummy()1474 public void Dummy () 1475 { 1476 protectedField = 1; 1477 } 1478 } 1479 1480 public class FieldInfoTest<T> 1481 { 1482 public T TestField; 1483 } 1484 } 1485