MonoProperty.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // System.Reflection/MonoProperty.cs
  3. // The class used to represent Properties from the mono runtime.
  4. //
  5. // Author:
  6. // Paolo Molaro ([email protected])
  7. // Patrik Torstensson ([email protected])
  8. //
  9. // (C) 2001 Ximian, Inc. http://www.ximian.com
  10. //
  11. using System;
  12. using System.Globalization;
  13. using System.Runtime.CompilerServices;
  14. using System.Runtime.InteropServices;
  15. namespace System.Reflection {
  16. internal struct MonoPropertyInfo {
  17. public Type parent;
  18. public String name;
  19. public MethodInfo get_method;
  20. public MethodInfo set_method;
  21. public PropertyAttributes attrs;
  22. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  23. internal static extern void get_property_info (MonoProperty prop, out MonoPropertyInfo info);
  24. }
  25. internal class MonoProperty : PropertyInfo {
  26. internal IntPtr klass;
  27. internal IntPtr prop;
  28. public override PropertyAttributes Attributes {
  29. get {
  30. MonoPropertyInfo info;
  31. MonoPropertyInfo.get_property_info (this, out info);
  32. return info.attrs;
  33. }
  34. }
  35. public override bool CanRead {
  36. get {
  37. MonoPropertyInfo info;
  38. MonoPropertyInfo.get_property_info (this, out info);
  39. return (info.get_method != null);
  40. }
  41. }
  42. public override bool CanWrite {
  43. get {
  44. MonoPropertyInfo info;
  45. MonoPropertyInfo.get_property_info (this, out info);
  46. return (info.set_method != null);
  47. }
  48. }
  49. public override Type PropertyType {
  50. get {
  51. MonoPropertyInfo info;
  52. MonoPropertyInfo.get_property_info (this, out info);
  53. if (info.get_method != null) {
  54. return info.get_method.ReturnType;
  55. } else {
  56. ParameterInfo[] parameters = info.set_method.GetParameters();
  57. return parameters [parameters.Length - 1].ParameterType;
  58. }
  59. }
  60. }
  61. public override Type ReflectedType {
  62. get {
  63. MonoPropertyInfo info;
  64. MonoPropertyInfo.get_property_info (this, out info);
  65. return info.parent;
  66. }
  67. }
  68. public override Type DeclaringType {
  69. get {
  70. MonoPropertyInfo info;
  71. MonoPropertyInfo.get_property_info (this, out info);
  72. return info.parent;
  73. }
  74. }
  75. public override string Name {
  76. get {
  77. MonoPropertyInfo info;
  78. MonoPropertyInfo.get_property_info (this, out info);
  79. return info.name;
  80. }
  81. }
  82. [MonoTODO]
  83. public override MethodInfo[] GetAccessors (bool nonPublic)
  84. {
  85. // FIXME: check nonPublic
  86. MonoPropertyInfo info;
  87. int n = 0;
  88. MonoPropertyInfo.get_property_info (this, out info);
  89. if (info.set_method != null)
  90. n++;
  91. if (info.get_method != null)
  92. n++;
  93. MethodInfo[] res = new MethodInfo [n];
  94. n = 0;
  95. if (info.set_method != null)
  96. res [n++] = info.set_method;
  97. if (info.get_method != null)
  98. res [n++] = info.get_method;
  99. return res;
  100. }
  101. [MonoTODO]
  102. public override MethodInfo GetGetMethod (bool nonPublic)
  103. {
  104. // FIXME: check nonPublic
  105. MonoPropertyInfo info;
  106. MonoPropertyInfo.get_property_info (this, out info);
  107. return info.get_method;
  108. }
  109. public override ParameterInfo[] GetIndexParameters()
  110. {
  111. MonoPropertyInfo info;
  112. MonoPropertyInfo.get_property_info (this, out info);
  113. if (info.get_method != null)
  114. return info.get_method.GetParameters ();
  115. return new ParameterInfo [0];
  116. }
  117. public override MethodInfo GetSetMethod (bool nonPublic)
  118. {
  119. // FIXME: check nonPublic
  120. MonoPropertyInfo info;
  121. MonoPropertyInfo.get_property_info (this, out info);
  122. return info.set_method;
  123. }
  124. public override bool IsDefined (Type attributeType, bool inherit)
  125. {
  126. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  127. }
  128. public override object[] GetCustomAttributes (bool inherit)
  129. {
  130. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  131. }
  132. public override object[] GetCustomAttributes (Type attributeType, bool inherit)
  133. {
  134. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  135. }
  136. public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  137. {
  138. object ret = null;
  139. MethodInfo method = GetGetMethod (false);
  140. if (method == null)
  141. throw new ArgumentException ("Get Method not found for '" + Name + "'");
  142. if (index == null || index.Length == 0)
  143. ret = method.Invoke (obj, invokeAttr, binder, null, culture);
  144. else
  145. ret = method.Invoke (obj, invokeAttr, binder, index, culture);
  146. return ret;
  147. }
  148. public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  149. {
  150. MethodInfo method = GetSetMethod (false);
  151. if (method == null)
  152. throw new ArgumentException ("Set Method not found for '" + Name + "'");
  153. object [] parms;
  154. if (index == null || index.Length == 0)
  155. parms = new object [] {value};
  156. else {
  157. int ilen = index.Length;
  158. parms = new object [ilen+ 1];
  159. index.CopyTo (parms, 0);
  160. parms [ilen] = value;
  161. }
  162. method.Invoke (obj, invokeAttr, binder, parms, culture);
  163. }
  164. public override string ToString () {
  165. return PropertyType.ToString () + " " + Name;
  166. }
  167. }
  168. }