MonoProperty.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Globalization;
  35. using System.Runtime.CompilerServices;
  36. using System.Runtime.InteropServices;
  37. namespace System.Reflection {
  38. internal struct MonoPropertyInfo {
  39. public Type parent;
  40. public String name;
  41. public MethodInfo get_method;
  42. public MethodInfo set_method;
  43. public PropertyAttributes attrs;
  44. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  45. internal static extern void get_property_info (MonoProperty prop, out MonoPropertyInfo info,
  46. PInfo req_info);
  47. }
  48. [Flags]
  49. internal enum PInfo {
  50. Attributes = 1,
  51. GetMethod = 1 << 1,
  52. SetMethod = 1 << 2,
  53. ReflectedType = 1 << 3,
  54. DeclaringType = 1 << 4,
  55. Name = 1 << 5
  56. }
  57. internal class MonoProperty : PropertyInfo {
  58. internal IntPtr klass;
  59. internal IntPtr prop;
  60. public override PropertyAttributes Attributes {
  61. get {
  62. MonoPropertyInfo info;
  63. MonoPropertyInfo.get_property_info (this, out info, PInfo.Attributes);
  64. return info.attrs;
  65. }
  66. }
  67. public override bool CanRead {
  68. get {
  69. MonoPropertyInfo info;
  70. MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
  71. return (info.get_method != null);
  72. }
  73. }
  74. public override bool CanWrite {
  75. get {
  76. MonoPropertyInfo info;
  77. MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
  78. return (info.set_method != null);
  79. }
  80. }
  81. public override Type PropertyType {
  82. get {
  83. MonoPropertyInfo info;
  84. MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
  85. if (info.get_method != null) {
  86. return info.get_method.ReturnType;
  87. } else {
  88. ParameterInfo[] parameters = info.set_method.GetParameters();
  89. return parameters [parameters.Length - 1].ParameterType;
  90. }
  91. }
  92. }
  93. public override Type ReflectedType {
  94. get {
  95. MonoPropertyInfo info;
  96. MonoPropertyInfo.get_property_info (this, out info, PInfo.ReflectedType);
  97. return info.parent;
  98. }
  99. }
  100. public override Type DeclaringType {
  101. get {
  102. MonoPropertyInfo info;
  103. MonoPropertyInfo.get_property_info (this, out info, PInfo.DeclaringType);
  104. return info.parent;
  105. }
  106. }
  107. public override string Name {
  108. get {
  109. MonoPropertyInfo info;
  110. MonoPropertyInfo.get_property_info (this, out info, PInfo.Name);
  111. return info.name;
  112. }
  113. }
  114. public override MethodInfo[] GetAccessors (bool nonPublic)
  115. {
  116. MonoPropertyInfo info;
  117. int nget = 0;
  118. int nset = 0;
  119. MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
  120. if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
  121. nset = 1;
  122. if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
  123. nget = 1;
  124. MethodInfo[] res = new MethodInfo [nget + nset];
  125. int n = 0;
  126. if (nset != 0)
  127. res [n++] = info.set_method;
  128. if (nget != 0)
  129. res [n++] = info.get_method;
  130. return res;
  131. }
  132. public override MethodInfo GetGetMethod (bool nonPublic)
  133. {
  134. MonoPropertyInfo info;
  135. MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
  136. if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
  137. return info.get_method;
  138. else
  139. return null;
  140. }
  141. public override ParameterInfo[] GetIndexParameters()
  142. {
  143. MonoPropertyInfo info;
  144. MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
  145. if (info.get_method != null)
  146. return info.get_method.GetParameters ();
  147. return new ParameterInfo [0];
  148. }
  149. public override MethodInfo GetSetMethod (bool nonPublic)
  150. {
  151. MonoPropertyInfo info;
  152. MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
  153. if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
  154. return info.set_method;
  155. else
  156. return null;
  157. }
  158. public override bool IsDefined (Type attributeType, bool inherit)
  159. {
  160. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  161. }
  162. public override object[] GetCustomAttributes (bool inherit)
  163. {
  164. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  165. }
  166. public override object[] GetCustomAttributes (Type attributeType, bool inherit)
  167. {
  168. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  169. }
  170. public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  171. {
  172. object ret = null;
  173. MethodInfo method = GetGetMethod (true);
  174. if (method == null)
  175. throw new ArgumentException ("Get Method not found for '" + Name + "'");
  176. if (index == null || index.Length == 0)
  177. ret = method.Invoke (obj, invokeAttr, binder, null, culture);
  178. else
  179. ret = method.Invoke (obj, invokeAttr, binder, index, culture);
  180. return ret;
  181. }
  182. public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  183. {
  184. MethodInfo method = GetSetMethod (true);
  185. if (method == null)
  186. throw new ArgumentException ("Set Method not found for '" + Name + "'");
  187. object [] parms;
  188. if (index == null || index.Length == 0)
  189. parms = new object [] {value};
  190. else {
  191. int ilen = index.Length;
  192. parms = new object [ilen+ 1];
  193. index.CopyTo (parms, 0);
  194. parms [ilen] = value;
  195. }
  196. method.Invoke (obj, invokeAttr, binder, parms, culture);
  197. }
  198. public override string ToString () {
  199. return PropertyType.ToString () + " " + Name;
  200. }
  201. #if NET_2_0 || BOOTSTRAP_NET_2_0
  202. [MonoTODO]
  203. public override Type[] OptionalCustomModifiers {
  204. get {
  205. throw new NotImplementedException ();
  206. }
  207. }
  208. [MonoTODO]
  209. public override Type[] RequiredCustomModifiers {
  210. get {
  211. throw new NotImplementedException ();
  212. }
  213. }
  214. #endif
  215. }
  216. }