MonoProperty.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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.Collections.Generic;
  32. using System.Globalization;
  33. using System.Runtime.CompilerServices;
  34. using System.Runtime.InteropServices;
  35. using System.Runtime.Serialization;
  36. using System.Security;
  37. namespace System.Reflection {
  38. internal struct MonoPropertyInfo {
  39. public Type parent;
  40. public Type declaring_type;
  41. public String name;
  42. public MethodInfo get_method;
  43. public MethodInfo set_method;
  44. public PropertyAttributes attrs;
  45. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  46. internal static extern void get_property_info (MonoProperty prop, ref MonoPropertyInfo info,
  47. PInfo req_info);
  48. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  49. internal static extern Type[] GetTypeModifiers (MonoProperty prop, bool optional);
  50. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  51. internal static extern object get_default_value (MonoProperty prop);
  52. }
  53. [Flags]
  54. internal enum PInfo {
  55. Attributes = 1,
  56. GetMethod = 1 << 1,
  57. SetMethod = 1 << 2,
  58. ReflectedType = 1 << 3,
  59. DeclaringType = 1 << 4,
  60. Name = 1 << 5
  61. }
  62. internal delegate object GetterAdapter (object _this);
  63. internal delegate R Getter<T,R> (T _this);
  64. [Serializable]
  65. internal class MonoProperty : PropertyInfo, ISerializable {
  66. #pragma warning disable 649
  67. internal IntPtr klass;
  68. internal IntPtr prop;
  69. MonoPropertyInfo info;
  70. PInfo cached;
  71. GetterAdapter cached_getter;
  72. #pragma warning restore 649
  73. void CachePropertyInfo (PInfo flags)
  74. {
  75. if ((cached & flags) != flags) {
  76. MonoPropertyInfo.get_property_info (this, ref info, flags);
  77. cached |= flags;
  78. }
  79. }
  80. public override PropertyAttributes Attributes {
  81. get {
  82. CachePropertyInfo (PInfo.Attributes);
  83. return info.attrs;
  84. }
  85. }
  86. public override bool CanRead {
  87. get {
  88. CachePropertyInfo (PInfo.GetMethod);
  89. return (info.get_method != null);
  90. }
  91. }
  92. public override bool CanWrite {
  93. get {
  94. CachePropertyInfo (PInfo.SetMethod);
  95. return (info.set_method != null);
  96. }
  97. }
  98. public override Type PropertyType {
  99. get {
  100. CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
  101. if (info.get_method != null) {
  102. return info.get_method.ReturnType;
  103. } else {
  104. ParameterInfo[] parameters = info.set_method.GetParameters ();
  105. return parameters [parameters.Length - 1].ParameterType;
  106. }
  107. }
  108. }
  109. public override Type ReflectedType {
  110. get {
  111. CachePropertyInfo (PInfo.ReflectedType);
  112. return info.parent;
  113. }
  114. }
  115. public override Type DeclaringType {
  116. get {
  117. CachePropertyInfo (PInfo.DeclaringType);
  118. return info.declaring_type;
  119. }
  120. }
  121. public override string Name {
  122. get {
  123. CachePropertyInfo (PInfo.Name);
  124. return info.name;
  125. }
  126. }
  127. public override MethodInfo[] GetAccessors (bool nonPublic)
  128. {
  129. int nget = 0;
  130. int nset = 0;
  131. CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
  132. if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
  133. nset = 1;
  134. if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
  135. nget = 1;
  136. MethodInfo[] res = new MethodInfo [nget + nset];
  137. int n = 0;
  138. if (nset != 0)
  139. res [n++] = info.set_method;
  140. if (nget != 0)
  141. res [n++] = info.get_method;
  142. return res;
  143. }
  144. public override MethodInfo GetGetMethod (bool nonPublic)
  145. {
  146. CachePropertyInfo (PInfo.GetMethod);
  147. if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
  148. return info.get_method;
  149. else
  150. return null;
  151. }
  152. public override ParameterInfo[] GetIndexParameters()
  153. {
  154. CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
  155. ParameterInfo[] res;
  156. if (info.get_method != null) {
  157. res = info.get_method.GetParameters ();
  158. } else if (info.set_method != null) {
  159. ParameterInfo[] src = info.set_method.GetParameters ();
  160. res = new ParameterInfo [src.Length - 1];
  161. Array.Copy (src, res, res.Length);
  162. } else
  163. return new ParameterInfo [0];
  164. for (int i = 0; i < res.Length; ++i) {
  165. ParameterInfo pinfo = res [i];
  166. res [i] = new ParameterInfo (pinfo, this);
  167. }
  168. return res;
  169. }
  170. public override MethodInfo GetSetMethod (bool nonPublic)
  171. {
  172. CachePropertyInfo (PInfo.SetMethod);
  173. if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
  174. return info.set_method;
  175. else
  176. return null;
  177. }
  178. /*TODO verify for attribute based default values, just like ParameterInfo*/
  179. public override object GetConstantValue ()
  180. {
  181. return MonoPropertyInfo.get_default_value (this);
  182. }
  183. public override object GetRawConstantValue() {
  184. return MonoPropertyInfo.get_default_value (this);
  185. }
  186. // According to MSDN the inherit parameter is ignored here and
  187. // the behavior always defaults to inherit = false
  188. //
  189. public override bool IsDefined (Type attributeType, bool inherit)
  190. {
  191. return MonoCustomAttrs.IsDefined (this, attributeType, false);
  192. }
  193. public override object[] GetCustomAttributes (bool inherit)
  194. {
  195. return MonoCustomAttrs.GetCustomAttributes (this, false);
  196. }
  197. public override object[] GetCustomAttributes (Type attributeType, bool inherit)
  198. {
  199. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, false);
  200. }
  201. delegate object GetterAdapter (object _this);
  202. delegate R Getter<T,R> (T _this);
  203. delegate R StaticGetter<R> ();
  204. #pragma warning disable 169
  205. // Used via reflection
  206. static object GetterAdapterFrame<T,R> (Getter<T,R> getter, object obj)
  207. {
  208. return getter ((T)obj);
  209. }
  210. static object StaticGetterAdapterFrame<R> (StaticGetter<R> getter, object obj)
  211. {
  212. return getter ();
  213. }
  214. #pragma warning restore 169
  215. /*
  216. * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
  217. * The first delegate cast the this argument to the right type and the second does points to the target method.
  218. */
  219. static GetterAdapter CreateGetterDelegate (MethodInfo method)
  220. {
  221. Type[] typeVector;
  222. Type getterType;
  223. object getterDelegate;
  224. MethodInfo adapterFrame;
  225. Type getterDelegateType;
  226. string frameName;
  227. if (method.IsStatic) {
  228. typeVector = new Type[] { method.ReturnType };
  229. getterDelegateType = typeof (StaticGetter<>);
  230. frameName = "StaticGetterAdapterFrame";
  231. } else {
  232. typeVector = new Type[] { method.DeclaringType, method.ReturnType };
  233. getterDelegateType = typeof (Getter<,>);
  234. frameName = "GetterAdapterFrame";
  235. }
  236. getterType = getterDelegateType.MakeGenericType (typeVector);
  237. #if NET_2_1
  238. // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
  239. // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
  240. // delegate that we can transform into a MethodAccessException
  241. getterDelegate = Delegate.CreateDelegate (getterType, method, false);
  242. if (getterDelegate == null)
  243. throw new MethodAccessException ();
  244. #else
  245. getterDelegate = Delegate.CreateDelegate (getterType, method);
  246. #endif
  247. adapterFrame = typeof (MonoProperty).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
  248. adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
  249. return (GetterAdapter)Delegate.CreateDelegate (typeof (GetterAdapter), getterDelegate, adapterFrame, true);
  250. }
  251. public override object GetValue (object obj, object[] index)
  252. {
  253. if (index == null || index.Length == 0) {
  254. /*FIXME we should check if the number of arguments matches the expected one, otherwise the error message will be pretty criptic.*/
  255. #if !MONOTOUCH
  256. if (cached_getter == null) {
  257. if (!DeclaringType.IsValueType) { //FIXME find a way to build an invoke delegate for value types.
  258. MethodInfo method = GetGetMethod (true);
  259. if (method == null)
  260. throw new ArgumentException ("Get Method not found for '" + Name + "'");
  261. cached_getter = CreateGetterDelegate (method);
  262. return cached_getter (obj);
  263. }
  264. } else {
  265. return cached_getter (obj);
  266. }
  267. #endif
  268. }
  269. return GetValue (obj, BindingFlags.Default, null, index, null);
  270. }
  271. public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  272. {
  273. object ret = null;
  274. MethodInfo method = GetGetMethod (true);
  275. if (method == null)
  276. throw new ArgumentException ("Get Method not found for '" + Name + "'");
  277. try {
  278. if (index == null || index.Length == 0)
  279. ret = method.Invoke (obj, invokeAttr, binder, null, culture);
  280. else
  281. ret = method.Invoke (obj, invokeAttr, binder, index, culture);
  282. }
  283. catch (SecurityException se) {
  284. throw new TargetInvocationException (se);
  285. }
  286. return ret;
  287. }
  288. public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  289. {
  290. MethodInfo method = GetSetMethod (true);
  291. if (method == null)
  292. throw new ArgumentException ("Set Method not found for '" + Name + "'");
  293. object [] parms;
  294. if (index == null || index.Length == 0)
  295. parms = new object [] {value};
  296. else {
  297. int ilen = index.Length;
  298. parms = new object [ilen+ 1];
  299. index.CopyTo (parms, 0);
  300. parms [ilen] = value;
  301. }
  302. method.Invoke (obj, invokeAttr, binder, parms, culture);
  303. }
  304. public override string ToString () {
  305. return PropertyType.ToString () + " " + Name;
  306. }
  307. public override Type[] GetOptionalCustomModifiers () {
  308. Type[] types = MonoPropertyInfo.GetTypeModifiers (this, true);
  309. if (types == null)
  310. return Type.EmptyTypes;
  311. return types;
  312. }
  313. public override Type[] GetRequiredCustomModifiers () {
  314. Type[] types = MonoPropertyInfo.GetTypeModifiers (this, false);
  315. if (types == null)
  316. return Type.EmptyTypes;
  317. return types;
  318. }
  319. // ISerializable
  320. public void GetObjectData (SerializationInfo info, StreamingContext context)
  321. {
  322. MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
  323. ToString(), MemberTypes.Property);
  324. }
  325. #if NET_4_0
  326. public override IList<CustomAttributeData> GetCustomAttributesData () {
  327. return CustomAttributeData.GetCustomAttributes (this);
  328. }
  329. #endif
  330. }
  331. }