FieldInfoTest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // FieldInfoTest - NUnit Test Cases for the FieldInfo class
  3. //
  4. // Zoltan Varga ([email protected])
  5. //
  6. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  7. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Threading;
  30. using System.Reflection;
  31. using System.Reflection.Emit;
  32. using System.Runtime.InteropServices;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Reflection
  35. {
  36. [StructLayout(LayoutKind.Explicit, Pack = 4, Size = 64)]
  37. public class Class1 {
  38. [FieldOffset (32)]
  39. public int i;
  40. }
  41. [StructLayout(LayoutKind.Sequential)]
  42. public class Class2 {
  43. [MarshalAsAttribute(UnmanagedType.Bool)]
  44. public int f0;
  45. [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]
  46. public string[] f1;
  47. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
  48. public string f2;
  49. // This doesn't work under mono
  50. //[MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")]
  51. //public object f3;
  52. }
  53. [TestFixture]
  54. public class FieldInfoTest : Assertion
  55. {
  56. [NonSerialized]
  57. public int i;
  58. #if NET_2_0
  59. [Test]
  60. public void PseudoCustomAttributes () {
  61. Type t = typeof (FieldInfoTest);
  62. AssertEquals (1, t.GetField ("i").GetCustomAttributes (typeof (NonSerializedAttribute), true).Length);
  63. FieldOffsetAttribute field_attr = (FieldOffsetAttribute)(typeof (Class1).GetField ("i").GetCustomAttributes (true) [0]);
  64. AssertEquals (32, field_attr.Value);
  65. MarshalAsAttribute attr;
  66. attr = (MarshalAsAttribute)typeof (Class2).GetField ("f0").GetCustomAttributes (true) [0];
  67. AssertEquals (UnmanagedType.Bool, attr.Value);
  68. attr = (MarshalAsAttribute)typeof (Class2).GetField ("f1").GetCustomAttributes (true) [0];
  69. AssertEquals (UnmanagedType.LPArray, attr.Value);
  70. AssertEquals (UnmanagedType.LPStr, attr.ArraySubType);
  71. attr = (MarshalAsAttribute)typeof (Class2).GetField ("f2").GetCustomAttributes (true) [0];
  72. AssertEquals (UnmanagedType.ByValTStr, attr.Value);
  73. AssertEquals (100, attr.SizeConst);
  74. /*
  75. attr = (MarshalAsAttribute)typeof (Class2).GetField ("f3").GetCustomAttributes (true) [0];
  76. AssertEquals (UnmanagedType.CustomMarshaler, attr.Value);
  77. AssertEquals ("5", attr.MarshalCookie);
  78. AssertEquals (typeof (Marshal1), Type.GetType (attr.MarshalType));
  79. */
  80. }
  81. [Test]
  82. [ExpectedException (typeof (InvalidOperationException))]
  83. public void GetValueOnRefOnlyAssembly ()
  84. {
  85. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
  86. Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName);
  87. FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic);
  88. f.GetValue (null);
  89. }
  90. [Test]
  91. [ExpectedException (typeof (InvalidOperationException))]
  92. public void SetValueOnRefOnlyAssembly ()
  93. {
  94. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
  95. Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName);
  96. FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic);
  97. f.SetValue (null, 8);
  98. }
  99. const int literal = 42;
  100. [Test]
  101. [ExpectedException (typeof (FieldAccessException))]
  102. public void SetValueOnLiteralField ()
  103. {
  104. FieldInfo f = typeof (FieldInfoTest).GetField ("literal", BindingFlags.Static | BindingFlags.NonPublic);
  105. f.SetValue (null, 0);
  106. }
  107. public int? nullable_field;
  108. public static int? static_nullable_field;
  109. [Test]
  110. public void NullableTests ()
  111. {
  112. FieldInfoTest t = new FieldInfoTest ();
  113. FieldInfo fi = typeof (FieldInfoTest).GetField ("nullable_field");
  114. fi.SetValue (t, 101);
  115. AssertEquals (101, fi.GetValue (t));
  116. fi.SetValue (t, null);
  117. AssertEquals (null, fi.GetValue (t));
  118. FieldInfo fi2 = typeof (FieldInfoTest).GetField ("static_nullable_field");
  119. fi2.SetValue (t, 101);
  120. AssertEquals (101, fi2.GetValue (t));
  121. fi2.SetValue (t, null);
  122. AssertEquals (null, fi2.GetValue (t));
  123. }
  124. #endif
  125. }
  126. #if NET_2_0
  127. // Helper class
  128. class RefOnlyFieldClass
  129. {
  130. // Helper property
  131. static int RefOnlyField;
  132. }
  133. #endif
  134. }