PropertyInfoTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 System.Threading;
  32. #if !MONOTOUCH
  33. using System.Reflection.Emit;
  34. #endif
  35. using System.IO;
  36. using NUnit.Framework;
  37. namespace MonoTests.System.Reflection
  38. {
  39. [TestFixture]
  40. public class PropertyInfoTest
  41. {
  42. [Test]
  43. public void GetAccessorsTest ()
  44. {
  45. Type type = typeof (TestClass);
  46. PropertyInfo property = type.GetProperty ("ReadOnlyProperty");
  47. MethodInfo [] methods = property.GetAccessors (true);
  48. Assert.AreEqual (1, methods.Length, "#A1");
  49. Assert.IsNotNull (methods [0], "#A2");
  50. Assert.AreEqual ("get_ReadOnlyProperty", methods [0].Name, "#A3");
  51. methods = property.GetAccessors (false);
  52. Assert.AreEqual (1, methods.Length, "#B1");
  53. Assert.IsNotNull (methods [0], "#B2");
  54. Assert.AreEqual ("get_ReadOnlyProperty", methods [0].Name, "#B3");
  55. property = typeof (Base).GetProperty ("P");
  56. methods = property.GetAccessors (true);
  57. Assert.AreEqual (2, methods.Length, "#C1");
  58. Assert.IsNotNull (methods [0], "#C2");
  59. Assert.IsNotNull (methods [1], "#C3");
  60. Assert.IsTrue (HasMethod (methods, "get_P"), "#C4");
  61. Assert.IsTrue (HasMethod (methods, "set_P"), "#C5");
  62. methods = property.GetAccessors (false);
  63. Assert.AreEqual (2, methods.Length, "#D1");
  64. Assert.IsNotNull (methods [0], "#D2");
  65. Assert.IsNotNull (methods [1], "#D3");
  66. Assert.IsTrue (HasMethod (methods, "get_P"), "#D4");
  67. Assert.IsTrue (HasMethod (methods, "set_P"), "#D5");
  68. methods = property.GetAccessors ();
  69. Assert.AreEqual (2, methods.Length, "#E1");
  70. Assert.IsNotNull (methods [0], "#E2");
  71. Assert.IsNotNull (methods [1], "#E3");
  72. Assert.IsTrue (HasMethod (methods, "get_P"), "#E4");
  73. Assert.IsTrue (HasMethod (methods, "set_P"), "#E5");
  74. property = typeof (TestClass).GetProperty ("Private",
  75. BindingFlags.NonPublic | BindingFlags.Instance);
  76. methods = property.GetAccessors (true);
  77. Assert.AreEqual (2, methods.Length, "#F1");
  78. Assert.IsNotNull (methods [0], "#F2");
  79. Assert.IsNotNull (methods [1], "#F3");
  80. Assert.IsTrue (HasMethod (methods, "get_Private"), "#F4");
  81. Assert.IsTrue (HasMethod (methods, "set_Private"), "#F5");
  82. methods = property.GetAccessors (false);
  83. Assert.AreEqual (0, methods.Length, "#G");
  84. methods = property.GetAccessors ();
  85. Assert.AreEqual (0, methods.Length, "#H");
  86. #if NET_2_0
  87. property = typeof (TestClass).GetProperty ("PrivateSetter");
  88. methods = property.GetAccessors (true);
  89. Assert.AreEqual (2, methods.Length, "#H1");
  90. Assert.IsNotNull (methods [0], "#H2");
  91. Assert.IsNotNull (methods [1], "#H3");
  92. Assert.IsTrue (HasMethod (methods, "get_PrivateSetter"), "#H4");
  93. Assert.IsTrue (HasMethod (methods, "set_PrivateSetter"), "#H5");
  94. methods = property.GetAccessors (false);
  95. Assert.AreEqual (1, methods.Length, "#I1");
  96. Assert.IsNotNull (methods [0], "#I2");
  97. Assert.AreEqual ("get_PrivateSetter", methods [0].Name, "#I3");
  98. methods = property.GetAccessors ();
  99. Assert.AreEqual (1, methods.Length, "#J1");
  100. Assert.IsNotNull (methods [0], "#J2");
  101. Assert.AreEqual ("get_PrivateSetter", methods [0].Name, "#J3");
  102. #endif
  103. }
  104. [Test]
  105. public void GetCustomAttributes ()
  106. {
  107. object [] attrs;
  108. PropertyInfo p = typeof (Base).GetProperty ("P");
  109. attrs = p.GetCustomAttributes (false);
  110. Assert.AreEqual (1, attrs.Length, "#A1");
  111. Assert.AreEqual (typeof (ThisAttribute), attrs [0].GetType (), "#A2");
  112. attrs = p.GetCustomAttributes (true);
  113. Assert.AreEqual (1, attrs.Length, "#A3");
  114. Assert.AreEqual (typeof (ThisAttribute), attrs [0].GetType (), "#A4");
  115. p = typeof (Base).GetProperty ("T");
  116. attrs = p.GetCustomAttributes (false);
  117. Assert.AreEqual (2, attrs.Length, "#B1");
  118. Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B2");
  119. Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B3");
  120. attrs = p.GetCustomAttributes (true);
  121. Assert.AreEqual (2, attrs.Length, "#B41");
  122. Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B5");
  123. Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B6");
  124. p = typeof (Base).GetProperty ("Z");
  125. attrs = p.GetCustomAttributes (false);
  126. Assert.AreEqual (0, attrs.Length, "#C1");
  127. attrs = p.GetCustomAttributes (true);
  128. Assert.AreEqual (0, attrs.Length, "#C2");
  129. }
  130. [Test]
  131. public void GetCustomAttributes_Inherited ()
  132. {
  133. object [] attrs;
  134. PropertyInfo p = typeof (Derived).GetProperty ("P");
  135. attrs = p.GetCustomAttributes (false);
  136. Assert.AreEqual (0, attrs.Length, "#A1");
  137. attrs = p.GetCustomAttributes (true);
  138. Assert.AreEqual (0, attrs.Length, "#A3");
  139. p = typeof (Derived).GetProperty ("T");
  140. attrs = p.GetCustomAttributes (false);
  141. Assert.AreEqual (2, attrs.Length, "#B1");
  142. Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B2");
  143. Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B3");
  144. attrs = p.GetCustomAttributes (true);
  145. Assert.AreEqual (2, attrs.Length, "#B41");
  146. Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B5");
  147. Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B6");
  148. p = typeof (Derived).GetProperty ("Z");
  149. attrs = p.GetCustomAttributes (false);
  150. Assert.AreEqual (0, attrs.Length, "#C1");
  151. attrs = p.GetCustomAttributes (true);
  152. Assert.AreEqual (0, attrs.Length, "#C2");
  153. }
  154. [Test]
  155. public void IsDefined_AttributeType_Null ()
  156. {
  157. Type derived = typeof (Derived);
  158. PropertyInfo pi = derived.GetProperty ("P");
  159. try {
  160. pi.IsDefined ((Type) null, false);
  161. Assert.Fail ("#1");
  162. } catch (ArgumentNullException ex) {
  163. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  164. Assert.IsNull (ex.InnerException, "#3");
  165. Assert.IsNotNull (ex.Message, "#4");
  166. Assert.IsNotNull (ex.ParamName, "#5");
  167. Assert.AreEqual ("attributeType", ex.ParamName, "#6");
  168. }
  169. }
  170. [Test]
  171. public void AccessorsReflectedType ()
  172. {
  173. PropertyInfo pi = typeof (Derived).GetProperty ("T");
  174. Assert.AreEqual (typeof (Derived), pi.GetGetMethod ().ReflectedType);
  175. Assert.AreEqual (typeof (Derived), pi.GetSetMethod ().ReflectedType);
  176. }
  177. [Test] // bug #399985
  178. public void SetValue_Enum ()
  179. {
  180. TestClass t = new TestClass ();
  181. PropertyInfo pi = t.GetType ().GetProperty ("Targets");
  182. pi.SetValue (t, AttributeTargets.Field, null);
  183. Assert.AreEqual (AttributeTargets.Field, t.Targets, "#1");
  184. pi.SetValue (t, (int) AttributeTargets.Interface, null);
  185. Assert.AreEqual (AttributeTargets.Interface, t.Targets, "#2");
  186. }
  187. public class ThisAttribute : Attribute
  188. {
  189. }
  190. class Base
  191. {
  192. [ThisAttribute]
  193. public virtual string P {
  194. get { return null; }
  195. set { }
  196. }
  197. [ThisAttribute]
  198. [ComVisible (false)]
  199. public virtual string T {
  200. get { return null; }
  201. set { }
  202. }
  203. public virtual string Z {
  204. get { return null; }
  205. set { }
  206. }
  207. }
  208. class Derived : Base
  209. {
  210. public override string P {
  211. get { return null; }
  212. set { }
  213. }
  214. }
  215. static void RunTest (Type t, bool use_getter) {
  216. var p = t.GetProperty ("Item");
  217. var idx = p.GetIndexParameters ();
  218. var m_args = t.GetMethod (use_getter ? "get_Item" : "set_Item").GetParameters ();
  219. Assert.AreEqual (2, idx.Length, "#1");
  220. Assert.AreEqual (typeof (double), idx [0].ParameterType, "#2");
  221. Assert.AreEqual (p, idx [0].Member, "#3");
  222. Assert.AreEqual ("a", idx [0].Name, "#4");
  223. Assert.AreEqual (0, idx [0].Position, "#5");
  224. Assert.AreEqual (m_args [0].MetadataToken, idx [0].MetadataToken, "#6");
  225. Assert.AreEqual (ParameterAttributes.None, idx [0].Attributes, "#7");
  226. Assert.AreEqual (typeof (string), idx [1].ParameterType, "#8");
  227. Assert.AreEqual (p, idx [1].Member, "#9");
  228. Assert.AreEqual ("b", idx [1].Name, "#10");
  229. Assert.AreEqual (1, idx [1].Position, "#11");
  230. Assert.AreEqual (m_args [1].MetadataToken, idx [1].MetadataToken, "#12");
  231. Assert.AreEqual (ParameterAttributes.None, idx [1].Attributes, "#13");
  232. var idx2 = p.GetIndexParameters ();
  233. //No interning exposed
  234. Assert.AreNotSame (idx, idx2, "#14");
  235. Assert.AreNotSame (idx [0], idx2 [1], "#15");
  236. }
  237. [Test]
  238. public void GetIndexParameterReturnsObjectsBoundToTheProperty ()
  239. {
  240. RunTest (typeof (TestA), false);
  241. RunTest (typeof (TestB), true);
  242. }
  243. public class TestA {
  244. public int this[double a, string b] {
  245. set {}
  246. }
  247. }
  248. public class TestB {
  249. public int this[double a, string b] {
  250. get { return 1; }
  251. set {}
  252. }
  253. }
  254. [Test]
  255. public void GetIndexParameterReturnedObjectsCustomAttributes () {
  256. var pa = typeof (TestC).GetProperty ("Item").GetIndexParameters () [0];
  257. Assert.IsTrue (pa.IsDefined (typeof (ParamArrayAttribute), false), "#1");
  258. var pb = typeof (TestD).GetProperty ("Item").GetIndexParameters () [0];
  259. Assert.IsTrue (pb.IsDefined (typeof (ParamArrayAttribute), false), "#2");
  260. Assert.AreEqual (1, Attribute.GetCustomAttributes (pa).Length, "#3");
  261. Assert.AreEqual (1, Attribute.GetCustomAttributes (pb).Length, "#4");
  262. Assert.AreEqual (0, pa.GetOptionalCustomModifiers ().Length, "#5");
  263. Assert.AreEqual (0, pb.GetRequiredCustomModifiers ().Length, "#6");
  264. }
  265. public class TestC {
  266. public int this[params double[] a] {
  267. get { return 99; }
  268. }
  269. }
  270. public class TestD {
  271. public int this[params double[] a] {
  272. set { }
  273. }
  274. }
  275. static string CreateTempAssembly ()
  276. {
  277. FileStream f = null;
  278. string path;
  279. Random rnd;
  280. int num = 0;
  281. rnd = new Random ();
  282. do {
  283. num = rnd.Next ();
  284. num++;
  285. path = Path.Combine (Path.GetTempPath (), "tmp" + num.ToString ("x") + ".dll");
  286. try {
  287. f = new FileStream (path, FileMode.CreateNew);
  288. } catch { }
  289. } while (f == null);
  290. f.Close ();
  291. return "tmp" + num.ToString ("x") + ".dll";
  292. }
  293. public class TestE {
  294. public int PropE {
  295. get { return 99; }
  296. }
  297. }
  298. #if !MONOTOUCH
  299. [Test]
  300. public void ConstantValue () {
  301. /*This test looks scary because we can't generate a default value with C# */
  302. var assemblyName = new AssemblyName ();
  303. assemblyName.Name = "MonoTests.System.Reflection.Emit.PropertyInfoTest";
  304. string an = CreateTempAssembly ();
  305. var assembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
  306. var module = assembly.DefineDynamicModule ("module1", an);
  307. var tb = module.DefineType ("Test", TypeAttributes.Public);
  308. var prop = tb.DefineProperty ("Prop", PropertyAttributes.HasDefault, typeof (string), new Type [0]);
  309. var getter = tb.DefineMethod ("get_Prop", MethodAttributes.Public, typeof (string), new Type [0]);
  310. var ilgen = getter.GetILGenerator ();
  311. ilgen.Emit (OpCodes.Ldnull);
  312. ilgen.Emit (OpCodes.Ret);
  313. var setter = tb.DefineMethod ("set_Prop", MethodAttributes.Public, null, new Type [1] { typeof (string) });
  314. setter.GetILGenerator ().Emit (OpCodes.Ret);
  315. prop.SetConstant ("test");
  316. prop.SetGetMethod (getter);
  317. prop.SetSetMethod (setter);
  318. tb.CreateType ();
  319. File.Delete (Path.Combine (Path.GetTempPath (), an));
  320. assembly.Save (an);
  321. var asm = Assembly.LoadFrom (Path.Combine (Path.GetTempPath (), an));
  322. var t = asm.GetType ("Test");
  323. var p = t.GetProperty ("Prop");
  324. Assert.AreEqual ("test", p.GetConstantValue (), "#1");
  325. File.Delete (Path.Combine (Path.GetTempPath (), an));
  326. var pa = typeof (TestE).GetProperty ("PropE");
  327. try {
  328. pa.GetConstantValue ();
  329. Assert.Fail ("#2");
  330. } catch (InvalidOperationException) {
  331. }
  332. }
  333. #endif
  334. #if NET_2_0
  335. public class A<T>
  336. {
  337. public string Property {
  338. get { return typeof (T).FullName; }
  339. }
  340. }
  341. public int? nullable_field;
  342. public int? NullableProperty {
  343. get { return nullable_field; }
  344. set { nullable_field = value; }
  345. }
  346. [Test]
  347. [Category ("MobileNotWorking")] // bug #10266
  348. public void NullableTests ()
  349. {
  350. PropertyInfoTest t = new PropertyInfoTest ();
  351. PropertyInfo pi = typeof(PropertyInfoTest).GetProperty("NullableProperty");
  352. pi.SetValue (t, 100, null);
  353. Assert.AreEqual (100, pi.GetValue (t, null));
  354. pi.SetValue (t, null, null);
  355. Assert.AreEqual (null, pi.GetValue (t, null));
  356. }
  357. [Test]
  358. public void Bug77160 ()
  359. {
  360. object instance = new A<string> ();
  361. Type type = instance.GetType ();
  362. PropertyInfo property = type.GetProperty ("Property");
  363. Assert.AreEqual (typeof (string).FullName, property.GetValue (instance, null));
  364. }
  365. #endif
  366. static bool HasAttribute (object [] attrs, Type attributeType)
  367. {
  368. foreach (object attr in attrs)
  369. if (attr.GetType () == attributeType)
  370. return true;
  371. return false;
  372. }
  373. static bool HasMethod (MethodInfo [] methods, string name)
  374. {
  375. foreach (MethodInfo method in methods)
  376. if (method.Name == name)
  377. return true;
  378. return false;
  379. }
  380. private class TestClass
  381. {
  382. private AttributeTargets _targets = AttributeTargets.Assembly;
  383. public AttributeTargets Targets {
  384. get { return _targets; }
  385. set { _targets = value; }
  386. }
  387. public string ReadOnlyProperty {
  388. get { return string.Empty; }
  389. }
  390. private string Private {
  391. get { return null; }
  392. set { }
  393. }
  394. #if NET_2_0
  395. public string PrivateSetter {
  396. get { return null; }
  397. private set { }
  398. }
  399. #endif
  400. }
  401. [Test] // bug #633671
  402. public void DeclaringTypeOfPropertyFromInheritedTypePointsToBase ()
  403. {
  404. var inherit1 = typeof(InheritsFromClassWithNullableDateTime);
  405. var siblingProperty = inherit1.GetProperty("Property1");
  406. Assert.AreEqual (typeof (ClassWithNullableDateTime), siblingProperty.DeclaringType, "#1");
  407. Assert.AreEqual (typeof (InheritsFromClassWithNullableDateTime), siblingProperty.ReflectedType, "#2");
  408. //The check is done twice since the bug is related to getting those 2 properties multiple times.
  409. Assert.AreEqual (typeof (ClassWithNullableDateTime), siblingProperty.DeclaringType, "#3");
  410. Assert.AreEqual (typeof (InheritsFromClassWithNullableDateTime), siblingProperty.ReflectedType, "#4");
  411. }
  412. public class ClassWithNullableDateTime
  413. {
  414. public DateTime? Property1 { get; set; }
  415. }
  416. public class InheritsFromClassWithNullableDateTime : ClassWithNullableDateTime
  417. {
  418. }
  419. public static int ThrowingProperty {
  420. get {
  421. throw new ObjectDisposedException("TestClass");
  422. }
  423. }
  424. [Test]
  425. public void GetException () {
  426. var prop = typeof(PropertyInfoTest).GetProperty("ThrowingProperty");
  427. try {
  428. prop.GetValue (null, null);
  429. Assert.Fail ();
  430. } catch (TargetInvocationException ex) {
  431. Assert.IsTrue (ex.InnerException is ObjectDisposedException);
  432. }
  433. }
  434. }
  435. }