PropertyInfoTest.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // PropertyInfoTest.cs - NUnit Test Cases for PropertyInfo
  3. //
  4. // Author:
  5. // Gert Driesen ([email protected])
  6. //
  7. // (C) 2004 Novell
  8. //
  9. using System;
  10. using System.Reflection;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.Reflection
  13. {
  14. [TestFixture]
  15. public class PropertyInfoTest
  16. {
  17. [Test]
  18. public void GetAccessorsTest()
  19. {
  20. Type type = typeof(TestClass);
  21. PropertyInfo property = type.GetProperty ("ReadOnlyProperty");
  22. MethodInfo[] methods = property.GetAccessors (true);
  23. Assert.AreEqual (1, methods.Length, "GetAccessors#1");
  24. Assert.IsNotNull (methods[0], "GetAccessors#2");
  25. }
  26. [Test]
  27. [Category ("NotWorking")]
  28. public void GetCustomAttributesInherited ()
  29. {
  30. Type derived = typeof (Derived);
  31. PropertyInfo p = derived.GetProperty ("P");
  32. Assert.AreEqual (1, p.GetCustomAttributes (true).Length);
  33. }
  34. public class ThisAttribute : Attribute {
  35. }
  36. class Base {
  37. [ThisAttribute]
  38. public virtual string P {
  39. get { return null; }
  40. set { }
  41. }
  42. }
  43. class Derived : Base {
  44. public override string P {
  45. get { return null; }
  46. set { }
  47. }
  48. }
  49. #if NET_2_0
  50. public class A<T>
  51. {
  52. public string Property {
  53. get { return typeof (T).FullName; }
  54. }
  55. }
  56. public int? nullable_field;
  57. public int? NullableProperty {
  58. get { return nullable_field; }
  59. set { nullable_field = value; }
  60. }
  61. [Test]
  62. public void NullableTests ()
  63. {
  64. PropertyInfoTest t = new PropertyInfoTest ();
  65. PropertyInfo pi = typeof(PropertyInfoTest).GetProperty("NullableProperty");
  66. pi.SetValue (t, 100, null);
  67. Assert.AreEqual (100, pi.GetValue (t, null));
  68. pi.SetValue (t, null, null);
  69. Assert.AreEqual (null, pi.GetValue (t, null));
  70. }
  71. [Test]
  72. public void Bug77160 ()
  73. {
  74. object instance = new A<string> ();
  75. Type type = instance.GetType ();
  76. PropertyInfo property = type.GetProperty ("Property");
  77. Assert.AreEqual (typeof (string).FullName, property.GetValue (instance, null));
  78. }
  79. #endif
  80. private class TestClass
  81. {
  82. public string ReadOnlyProperty
  83. {
  84. get { return string.Empty; }
  85. }
  86. }
  87. }
  88. }