MonoProperty.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. PInfo req_info);
  25. }
  26. [Flags]
  27. internal enum PInfo {
  28. Attributes = 1,
  29. GetMethod = 1 << 1,
  30. SetMethod = 1 << 2,
  31. ReflectedType = 1 << 3,
  32. DeclaringType = 1 << 4,
  33. Name = 1 << 5
  34. }
  35. internal class MonoProperty : PropertyInfo {
  36. internal IntPtr klass;
  37. internal IntPtr prop;
  38. public override PropertyAttributes Attributes {
  39. get {
  40. MonoPropertyInfo info;
  41. MonoPropertyInfo.get_property_info (this, out info, PInfo.Attributes);
  42. return info.attrs;
  43. }
  44. }
  45. public override bool CanRead {
  46. get {
  47. MonoPropertyInfo info;
  48. MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
  49. return (info.get_method != null);
  50. }
  51. }
  52. public override bool CanWrite {
  53. get {
  54. MonoPropertyInfo info;
  55. MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
  56. return (info.set_method != null);
  57. }
  58. }
  59. public override Type PropertyType {
  60. get {
  61. MonoPropertyInfo info;
  62. MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
  63. if (info.get_method != null) {
  64. return info.get_method.ReturnType;
  65. } else {
  66. ParameterInfo[] parameters = info.set_method.GetParameters();
  67. return parameters [parameters.Length - 1].ParameterType;
  68. }
  69. }
  70. }
  71. public override Type ReflectedType {
  72. get {
  73. MonoPropertyInfo info;
  74. MonoPropertyInfo.get_property_info (this, out info, PInfo.ReflectedType);
  75. return info.parent;
  76. }
  77. }
  78. public override Type DeclaringType {
  79. get {
  80. MonoPropertyInfo info;
  81. MonoPropertyInfo.get_property_info (this, out info, PInfo.DeclaringType);
  82. return info.parent;
  83. }
  84. }
  85. public override string Name {
  86. get {
  87. MonoPropertyInfo info;
  88. MonoPropertyInfo.get_property_info (this, out info, PInfo.Name);
  89. return info.name;
  90. }
  91. }
  92. [MonoTODO]
  93. public override MethodInfo[] GetAccessors (bool nonPublic)
  94. {
  95. // FIXME: check nonPublic
  96. MonoPropertyInfo info;
  97. int n = 0;
  98. MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
  99. if (info.set_method != null)
  100. n++;
  101. if (info.get_method != null)
  102. n++;
  103. MethodInfo[] res = new MethodInfo [n];
  104. n = 0;
  105. if (info.set_method != null)
  106. res [n++] = info.set_method;
  107. if (info.get_method != null)
  108. res [n++] = info.get_method;
  109. return res;
  110. }
  111. [MonoTODO]
  112. public override MethodInfo GetGetMethod (bool nonPublic)
  113. {
  114. // FIXME: check nonPublic
  115. MonoPropertyInfo info;
  116. MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
  117. return info.get_method;
  118. }
  119. public override ParameterInfo[] GetIndexParameters()
  120. {
  121. MonoPropertyInfo info;
  122. MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
  123. if (info.get_method != null)
  124. return info.get_method.GetParameters ();
  125. return new ParameterInfo [0];
  126. }
  127. public override MethodInfo GetSetMethod (bool nonPublic)
  128. {
  129. // FIXME: check nonPublic
  130. MonoPropertyInfo info;
  131. MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
  132. return info.set_method;
  133. }
  134. public override bool IsDefined (Type attributeType, bool inherit)
  135. {
  136. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  137. }
  138. public override object[] GetCustomAttributes (bool inherit)
  139. {
  140. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  141. }
  142. public override object[] GetCustomAttributes (Type attributeType, bool inherit)
  143. {
  144. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  145. }
  146. public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  147. {
  148. object ret = null;
  149. MethodInfo method = GetGetMethod (false);
  150. if (method == null)
  151. throw new ArgumentException ("Get Method not found for '" + Name + "'");
  152. if (index == null || index.Length == 0)
  153. ret = method.Invoke (obj, invokeAttr, binder, null, culture);
  154. else
  155. ret = method.Invoke (obj, invokeAttr, binder, index, culture);
  156. return ret;
  157. }
  158. public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  159. {
  160. MethodInfo method = GetSetMethod (false);
  161. if (method == null)
  162. throw new ArgumentException ("Set Method not found for '" + Name + "'");
  163. object [] parms;
  164. if (index == null || index.Length == 0)
  165. parms = new object [] {value};
  166. else {
  167. int ilen = index.Length;
  168. parms = new object [ilen+ 1];
  169. index.CopyTo (parms, 0);
  170. parms [ilen] = value;
  171. }
  172. method.Invoke (obj, invokeAttr, binder, parms, culture);
  173. }
  174. public override string ToString () {
  175. return PropertyType.ToString () + " " + Name;
  176. }
  177. }
  178. }