FieldInfoTest.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #if !TARGET_JVM
  32. using System.Reflection.Emit;
  33. #endif // TARGET_JVM
  34. using System.Runtime.InteropServices;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Reflection
  37. {
  38. [StructLayout(LayoutKind.Explicit, Pack = 4, Size = 64)]
  39. public class Class1 {
  40. [FieldOffset (32)]
  41. public int i;
  42. }
  43. [StructLayout(LayoutKind.Sequential)]
  44. public class Class2 {
  45. [MarshalAsAttribute(UnmanagedType.Bool)]
  46. public int f0;
  47. [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]
  48. public string[] f1;
  49. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
  50. public string f2;
  51. // This doesn't work under mono
  52. //[MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")]
  53. //public object f3;
  54. }
  55. [TestFixture]
  56. public class FieldInfoTest : Assertion
  57. {
  58. [NonSerialized]
  59. public int i;
  60. #if NET_2_0
  61. [Test]
  62. public void PseudoCustomAttributes () {
  63. Type t = typeof (FieldInfoTest);
  64. AssertEquals (1, t.GetField ("i").GetCustomAttributes (typeof (NonSerializedAttribute), true).Length);
  65. FieldOffsetAttribute field_attr = (FieldOffsetAttribute)(typeof (Class1).GetField ("i").GetCustomAttributes (true) [0]);
  66. AssertEquals (32, field_attr.Value);
  67. MarshalAsAttribute attr;
  68. attr = (MarshalAsAttribute)typeof (Class2).GetField ("f0").GetCustomAttributes (true) [0];
  69. AssertEquals (UnmanagedType.Bool, attr.Value);
  70. attr = (MarshalAsAttribute)typeof (Class2).GetField ("f1").GetCustomAttributes (true) [0];
  71. AssertEquals (UnmanagedType.LPArray, attr.Value);
  72. AssertEquals (UnmanagedType.LPStr, attr.ArraySubType);
  73. attr = (MarshalAsAttribute)typeof (Class2).GetField ("f2").GetCustomAttributes (true) [0];
  74. AssertEquals (UnmanagedType.ByValTStr, attr.Value);
  75. AssertEquals (100, attr.SizeConst);
  76. /*
  77. attr = (MarshalAsAttribute)typeof (Class2).GetField ("f3").GetCustomAttributes (true) [0];
  78. AssertEquals (UnmanagedType.CustomMarshaler, attr.Value);
  79. AssertEquals ("5", attr.MarshalCookie);
  80. AssertEquals (typeof (Marshal1), Type.GetType (attr.MarshalType));
  81. */
  82. }
  83. #if !TARGET_JVM // ReflectionOnlyLoad not supported for TARGET_JVM
  84. [Test]
  85. [ExpectedException (typeof (InvalidOperationException))]
  86. public void GetValueOnRefOnlyAssembly ()
  87. {
  88. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
  89. Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName);
  90. FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic);
  91. f.GetValue (null);
  92. }
  93. [Test]
  94. [ExpectedException (typeof (InvalidOperationException))]
  95. public void SetValueOnRefOnlyAssembly ()
  96. {
  97. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
  98. Type t = assembly.GetType (typeof (RefOnlyFieldClass).FullName);
  99. FieldInfo f = t.GetField ("RefOnlyField", BindingFlags.Static | BindingFlags.NonPublic);
  100. f.SetValue (null, 8);
  101. }
  102. #endif // TARGET_JVM
  103. const int literal = 42;
  104. [Test]
  105. [ExpectedException (typeof (FieldAccessException))]
  106. public void SetValueOnLiteralField ()
  107. {
  108. FieldInfo f = typeof (FieldInfoTest).GetField ("literal", BindingFlags.Static | BindingFlags.NonPublic);
  109. f.SetValue (null, 0);
  110. }
  111. public int? nullable_field;
  112. public static int? static_nullable_field;
  113. [Test]
  114. public void NullableTests ()
  115. {
  116. FieldInfoTest t = new FieldInfoTest ();
  117. FieldInfo fi = typeof (FieldInfoTest).GetField ("nullable_field");
  118. fi.SetValue (t, 101);
  119. AssertEquals (101, fi.GetValue (t));
  120. fi.SetValue (t, null);
  121. AssertEquals (null, fi.GetValue (t));
  122. FieldInfo fi2 = typeof (FieldInfoTest).GetField ("static_nullable_field");
  123. fi2.SetValue (t, 101);
  124. AssertEquals (101, fi2.GetValue (t));
  125. fi2.SetValue (t, null);
  126. AssertEquals (null, fi2.GetValue (t));
  127. }
  128. #if !TARGET_JVM // TypeBuilder not supported for TARGET_JVM
  129. [Test]
  130. public void NonPublicTests ()
  131. {
  132. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (FieldInfoTest).Assembly.FullName);
  133. Type t = assembly.GetType (typeof (NonPublicFieldClass).FullName);
  134. // try to get non-public field
  135. FieldInfo fi = t.GetField ("protectedField");
  136. AssertNull (fi);
  137. // get it for real
  138. fi = t.GetField ("protectedField", BindingFlags.NonPublic | BindingFlags.Instance);
  139. AssertNotNull (fi);
  140. // get via typebuilder
  141. FieldInfo f = TypeBuilder.GetField (t, fi);
  142. AssertNotNull (f);
  143. }
  144. #endif // TARGET_JVM
  145. #endif
  146. }
  147. #if NET_2_0
  148. // Helper classes
  149. class RefOnlyFieldClass
  150. {
  151. // Helper property
  152. static int RefOnlyField;
  153. }
  154. class NonPublicFieldClass
  155. {
  156. protected int protectedField;
  157. }
  158. #endif
  159. }