PropertyInfoTest.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #if NET_2_0
  27. public class A<T>
  28. {
  29. public string Property {
  30. get { return typeof (T).FullName; }
  31. }
  32. }
  33. public int? nullable_field;
  34. public int? NullableProperty {
  35. get { return nullable_field; }
  36. set { nullable_field = value; }
  37. }
  38. [Test]
  39. public void NullableTests ()
  40. {
  41. PropertyInfoTest t = new PropertyInfoTest ();
  42. PropertyInfo pi = typeof(PropertyInfoTest).GetProperty("NullableProperty");
  43. pi.SetValue (t, 100, null);
  44. Assert.AreEqual (100, pi.GetValue (t, null));
  45. pi.SetValue (t, null, null);
  46. Assert.AreEqual (null, pi.GetValue (t, null));
  47. }
  48. [Test]
  49. public void Bug77160 ()
  50. {
  51. object instance = new A<string> ();
  52. Type type = instance.GetType ();
  53. PropertyInfo property = type.GetProperty ("Property");
  54. Assert.AreEqual (typeof (string).FullName, property.GetValue (instance, null));
  55. }
  56. #endif
  57. private class TestClass
  58. {
  59. public string ReadOnlyProperty
  60. {
  61. get { return string.Empty; }
  62. }
  63. }
  64. }
  65. }