MonoProperty.cs 6.8 KB

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