FieldInfoTest.cs 56 KB

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