PropertyInfoTest.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // PropertyInfoTest.cs - NUnit Test Cases for PropertyInfo
  3. //
  4. // Author:
  5. // Gert Driesen ([email protected])
  6. //
  7. // (C) 2004-2007 Gert Driesen
  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.Reflection;
  30. using System.Runtime.InteropServices;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Reflection
  33. {
  34. [TestFixture]
  35. public class PropertyInfoTest
  36. {
  37. [Test]
  38. public void GetAccessorsTest ()
  39. {
  40. Type type = typeof (TestClass);
  41. PropertyInfo property = type.GetProperty ("ReadOnlyProperty");
  42. MethodInfo [] methods = property.GetAccessors (true);
  43. Assert.AreEqual (1, methods.Length, "#A1");
  44. Assert.IsNotNull (methods [0], "#A2");
  45. Assert.AreEqual ("get_ReadOnlyProperty", methods [0].Name, "#A3");
  46. methods = property.GetAccessors (false);
  47. Assert.AreEqual (1, methods.Length, "#B1");
  48. Assert.IsNotNull (methods [0], "#B2");
  49. Assert.AreEqual ("get_ReadOnlyProperty", methods [0].Name, "#B3");
  50. property = typeof (Base).GetProperty ("P");
  51. methods = property.GetAccessors (true);
  52. Assert.AreEqual (2, methods.Length, "#C1");
  53. Assert.IsNotNull (methods [0], "#C2");
  54. Assert.IsNotNull (methods [1], "#C3");
  55. Assert.IsTrue (HasMethod (methods, "get_P"), "#C4");
  56. Assert.IsTrue (HasMethod (methods, "set_P"), "#C5");
  57. methods = property.GetAccessors (false);
  58. Assert.AreEqual (2, methods.Length, "#D1");
  59. Assert.IsNotNull (methods [0], "#D2");
  60. Assert.IsNotNull (methods [1], "#D3");
  61. Assert.IsTrue (HasMethod (methods, "get_P"), "#D4");
  62. Assert.IsTrue (HasMethod (methods, "set_P"), "#D5");
  63. methods = property.GetAccessors ();
  64. Assert.AreEqual (2, methods.Length, "#E1");
  65. Assert.IsNotNull (methods [0], "#E2");
  66. Assert.IsNotNull (methods [1], "#E3");
  67. Assert.IsTrue (HasMethod (methods, "get_P"), "#E4");
  68. Assert.IsTrue (HasMethod (methods, "set_P"), "#E5");
  69. property = typeof (TestClass).GetProperty ("Private",
  70. BindingFlags.NonPublic | BindingFlags.Instance);
  71. methods = property.GetAccessors (true);
  72. Assert.AreEqual (2, methods.Length, "#F1");
  73. Assert.IsNotNull (methods [0], "#F2");
  74. Assert.IsNotNull (methods [1], "#F3");
  75. Assert.IsTrue (HasMethod (methods, "get_Private"), "#F4");
  76. Assert.IsTrue (HasMethod (methods, "set_Private"), "#F5");
  77. methods = property.GetAccessors (false);
  78. Assert.AreEqual (0, methods.Length, "#G");
  79. methods = property.GetAccessors ();
  80. Assert.AreEqual (0, methods.Length, "#H");
  81. #if NET_2_0
  82. property = typeof (TestClass).GetProperty ("PrivateSetter");
  83. methods = property.GetAccessors (true);
  84. Assert.AreEqual (2, methods.Length, "#H1");
  85. Assert.IsNotNull (methods [0], "#H2");
  86. Assert.IsNotNull (methods [1], "#H3");
  87. Assert.IsTrue (HasMethod (methods, "get_PrivateSetter"), "#H4");
  88. Assert.IsTrue (HasMethod (methods, "set_PrivateSetter"), "#H5");
  89. methods = property.GetAccessors (false);
  90. Assert.AreEqual (1, methods.Length, "#I1");
  91. Assert.IsNotNull (methods [0], "#I2");
  92. Assert.AreEqual ("get_PrivateSetter", methods [0].Name, "#I3");
  93. methods = property.GetAccessors ();
  94. Assert.AreEqual (1, methods.Length, "#J1");
  95. Assert.IsNotNull (methods [0], "#J2");
  96. Assert.AreEqual ("get_PrivateSetter", methods [0].Name, "#J3");
  97. #endif
  98. }
  99. [Test]
  100. public void GetCustomAttributes ()
  101. {
  102. object [] attrs;
  103. PropertyInfo p = typeof (Base).GetProperty ("P");
  104. attrs = p.GetCustomAttributes (false);
  105. Assert.AreEqual (1, attrs.Length, "#A1");
  106. Assert.AreEqual (typeof (ThisAttribute), attrs [0].GetType (), "#A2");
  107. attrs = p.GetCustomAttributes (true);
  108. Assert.AreEqual (1, attrs.Length, "#A3");
  109. Assert.AreEqual (typeof (ThisAttribute), attrs [0].GetType (), "#A4");
  110. p = typeof (Base).GetProperty ("T");
  111. attrs = p.GetCustomAttributes (false);
  112. Assert.AreEqual (2, attrs.Length, "#B1");
  113. Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B2");
  114. Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B3");
  115. attrs = p.GetCustomAttributes (true);
  116. Assert.AreEqual (2, attrs.Length, "#B41");
  117. Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B5");
  118. Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B6");
  119. p = typeof (Base).GetProperty ("Z");
  120. attrs = p.GetCustomAttributes (false);
  121. Assert.AreEqual (0, attrs.Length, "#C1");
  122. attrs = p.GetCustomAttributes (true);
  123. Assert.AreEqual (0, attrs.Length, "#C2");
  124. }
  125. [Test]
  126. public void GetCustomAttributes_Inherited ()
  127. {
  128. object [] attrs;
  129. PropertyInfo p = typeof (Derived).GetProperty ("P");
  130. attrs = p.GetCustomAttributes (false);
  131. Assert.AreEqual (0, attrs.Length, "#A1");
  132. attrs = p.GetCustomAttributes (true);
  133. Assert.AreEqual (0, attrs.Length, "#A3");
  134. p = typeof (Derived).GetProperty ("T");
  135. attrs = p.GetCustomAttributes (false);
  136. Assert.AreEqual (2, attrs.Length, "#B1");
  137. Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B2");
  138. Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B3");
  139. attrs = p.GetCustomAttributes (true);
  140. Assert.AreEqual (2, attrs.Length, "#B41");
  141. Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B5");
  142. Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B6");
  143. p = typeof (Derived).GetProperty ("Z");
  144. attrs = p.GetCustomAttributes (false);
  145. Assert.AreEqual (0, attrs.Length, "#C1");
  146. attrs = p.GetCustomAttributes (true);
  147. Assert.AreEqual (0, attrs.Length, "#C2");
  148. }
  149. [Test]
  150. public void IsDefined_AttributeType_Null ()
  151. {
  152. Type derived = typeof (Derived);
  153. PropertyInfo pi = derived.GetProperty ("P");
  154. try {
  155. pi.IsDefined ((Type) null, false);
  156. Assert.Fail ("#1");
  157. } catch (ArgumentNullException ex) {
  158. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  159. Assert.IsNull (ex.InnerException, "#3");
  160. Assert.IsNotNull (ex.Message, "#4");
  161. Assert.IsNotNull (ex.ParamName, "#5");
  162. Assert.AreEqual ("attributeType", ex.ParamName, "#6");
  163. }
  164. }
  165. [Test]
  166. public void AccessorsReflectedType ()
  167. {
  168. PropertyInfo pi = typeof (Derived).GetProperty ("T");
  169. Assert.AreEqual (typeof (Derived), pi.GetGetMethod ().ReflectedType);
  170. Assert.AreEqual (typeof (Derived), pi.GetSetMethod ().ReflectedType);
  171. }
  172. public class ThisAttribute : Attribute
  173. {
  174. }
  175. class Base
  176. {
  177. [ThisAttribute]
  178. public virtual string P {
  179. get { return null; }
  180. set { }
  181. }
  182. [ThisAttribute]
  183. [ComVisible (false)]
  184. public virtual string T {
  185. get { return null; }
  186. set { }
  187. }
  188. public virtual string Z {
  189. get { return null; }
  190. set { }
  191. }
  192. }
  193. class Derived : Base
  194. {
  195. public override string P {
  196. get { return null; }
  197. set { }
  198. }
  199. }
  200. #if NET_2_0
  201. public class A<T>
  202. {
  203. public string Property {
  204. get { return typeof (T).FullName; }
  205. }
  206. }
  207. public int? nullable_field;
  208. public int? NullableProperty {
  209. get { return nullable_field; }
  210. set { nullable_field = value; }
  211. }
  212. [Test]
  213. public void NullableTests ()
  214. {
  215. PropertyInfoTest t = new PropertyInfoTest ();
  216. PropertyInfo pi = typeof(PropertyInfoTest).GetProperty("NullableProperty");
  217. pi.SetValue (t, 100, null);
  218. Assert.AreEqual (100, pi.GetValue (t, null));
  219. pi.SetValue (t, null, null);
  220. Assert.AreEqual (null, pi.GetValue (t, null));
  221. }
  222. [Test]
  223. public void Bug77160 ()
  224. {
  225. object instance = new A<string> ();
  226. Type type = instance.GetType ();
  227. PropertyInfo property = type.GetProperty ("Property");
  228. Assert.AreEqual (typeof (string).FullName, property.GetValue (instance, null));
  229. }
  230. #endif
  231. static bool HasAttribute (object [] attrs, Type attributeType)
  232. {
  233. foreach (object attr in attrs)
  234. if (attr.GetType () == attributeType)
  235. return true;
  236. return false;
  237. }
  238. static bool HasMethod (MethodInfo [] methods, string name)
  239. {
  240. foreach (MethodInfo method in methods)
  241. if (method.Name == name)
  242. return true;
  243. return false;
  244. }
  245. private class TestClass
  246. {
  247. public string ReadOnlyProperty {
  248. get { return string.Empty; }
  249. }
  250. private string Private {
  251. get { return null; }
  252. set { }
  253. }
  254. #if NET_2_0
  255. public string PrivateSetter {
  256. get { return null; }
  257. private set { }
  258. }
  259. #endif
  260. }
  261. }
  262. }