2 // MonoProperty.cs: The class used to represent Properties from the mono runtime.
5 // Paolo Molaro (lupus@ximian.com)
6 // Patrik Torstensson (patrik.torstensson@labs2.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // (C) 2001 Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 // Copyright 2013 Xamarin, Inc (http://www.xamarin.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections.Generic;
34 using System.Globalization;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization;
38 using System.Security;
41 namespace System.Reflection {
43 internal struct MonoPropertyInfo {
45 public Type declaring_type;
47 public MethodInfo get_method;
48 public MethodInfo set_method;
49 public PropertyAttributes attrs;
51 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52 internal static extern void get_property_info (MonoProperty prop, ref MonoPropertyInfo info,
55 [MethodImplAttribute (MethodImplOptions.InternalCall)]
56 internal static extern Type[] GetTypeModifiers (MonoProperty prop, bool optional);
58 [MethodImplAttribute (MethodImplOptions.InternalCall)]
59 internal static extern object get_default_value (MonoProperty prop);
67 ReflectedType = 1 << 3,
68 DeclaringType = 1 << 4,
73 internal delegate object GetterAdapter (object _this);
74 internal delegate R Getter<T,R> (T _this);
77 [StructLayout (LayoutKind.Sequential)]
78 internal class MonoProperty : PropertyInfo, ISerializable {
79 #pragma warning disable 649
80 internal IntPtr klass;
82 MonoPropertyInfo info;
84 GetterAdapter cached_getter;
86 #pragma warning restore 649
88 void CachePropertyInfo (PInfo flags)
90 if ((cached & flags) != flags) {
91 MonoPropertyInfo.get_property_info (this, ref info, flags);
96 public override PropertyAttributes Attributes {
98 CachePropertyInfo (PInfo.Attributes);
103 public override bool CanRead {
105 CachePropertyInfo (PInfo.GetMethod);
106 return (info.get_method != null);
110 public override bool CanWrite {
112 CachePropertyInfo (PInfo.SetMethod);
113 return (info.set_method != null);
117 public override Type PropertyType {
119 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
121 if (info.get_method != null) {
122 return info.get_method.ReturnType;
124 ParameterInfo[] parameters = info.set_method.GetParametersInternal ();
126 return parameters [parameters.Length - 1].ParameterType;
131 public override Type ReflectedType {
133 CachePropertyInfo (PInfo.ReflectedType);
138 public override Type DeclaringType {
140 CachePropertyInfo (PInfo.DeclaringType);
141 return info.declaring_type;
145 public override string Name {
147 CachePropertyInfo (PInfo.Name);
152 public override MethodInfo[] GetAccessors (bool nonPublic)
157 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
159 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
161 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
164 MethodInfo[] res = new MethodInfo [nget + nset];
167 res [n++] = info.set_method;
169 res [n++] = info.get_method;
173 public override MethodInfo GetGetMethod (bool nonPublic)
175 CachePropertyInfo (PInfo.GetMethod);
176 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
177 return info.get_method;
182 public override ParameterInfo[] GetIndexParameters ()
184 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
187 if (info.get_method != null) {
188 src = info.get_method.GetParametersInternal ();
190 } else if (info.set_method != null) {
191 src = info.set_method.GetParametersInternal ();
192 length = src.Length - 1;
194 return EmptyArray<ParameterInfo>.Value;
196 var dest = new ParameterInfo [length];
197 for (int i = 0; i < length; ++i) {
198 dest [i] = ParameterInfo.New (src [i], this);
203 public override MethodInfo GetSetMethod (bool nonPublic)
205 CachePropertyInfo (PInfo.SetMethod);
206 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
207 return info.set_method;
213 /*TODO verify for attribute based default values, just like ParameterInfo*/
214 public override object GetConstantValue ()
216 return MonoPropertyInfo.get_default_value (this);
219 public override object GetRawConstantValue() {
220 return MonoPropertyInfo.get_default_value (this);
223 // According to MSDN the inherit parameter is ignored here and
224 // the behavior always defaults to inherit = false
226 public override bool IsDefined (Type attributeType, bool inherit)
228 return MonoCustomAttrs.IsDefined (this, attributeType, false);
231 public override object[] GetCustomAttributes (bool inherit)
233 return MonoCustomAttrs.GetCustomAttributes (this, false);
236 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
238 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, false);
242 delegate object GetterAdapter (object _this);
243 delegate R Getter<T,R> (T _this);
244 delegate R StaticGetter<R> ();
246 #pragma warning disable 169
247 // Used via reflection
248 static object GetterAdapterFrame<T,R> (Getter<T,R> getter, object obj)
250 return getter ((T)obj);
253 static object StaticGetterAdapterFrame<R> (StaticGetter<R> getter, object obj)
257 #pragma warning restore 169
260 * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
261 * The first delegate cast the this argument to the right type and the second does points to the target method.
263 static GetterAdapter CreateGetterDelegate (MethodInfo method)
267 object getterDelegate;
268 MethodInfo adapterFrame;
269 Type getterDelegateType;
272 if (method.IsStatic) {
273 typeVector = new Type[] { method.ReturnType };
274 getterDelegateType = typeof (StaticGetter<>);
275 frameName = "StaticGetterAdapterFrame";
277 typeVector = new Type[] { method.DeclaringType, method.ReturnType };
278 getterDelegateType = typeof (Getter<,>);
279 frameName = "GetterAdapterFrame";
282 getterType = getterDelegateType.MakeGenericType (typeVector);
284 // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
285 // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
286 // delegate that we can transform into a MethodAccessException
287 getterDelegate = Delegate.CreateDelegate (getterType, method, false);
288 if (getterDelegate == null)
289 throw new MethodAccessException ();
291 getterDelegate = Delegate.CreateDelegate (getterType, method);
293 adapterFrame = typeof (MonoProperty).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
294 adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
295 return (GetterAdapter)Delegate.CreateDelegate (typeof (GetterAdapter), getterDelegate, adapterFrame, true);
298 public override object GetValue (object obj, object[] index)
300 if (index == null || index.Length == 0) {
301 /*FIXME we should check if the number of arguments matches the expected one, otherwise the error message will be pretty criptic.*/
302 #if !FULL_AOT_RUNTIME
303 if (cached_getter == null) {
304 MethodInfo method = GetGetMethod (true);
305 if (!DeclaringType.IsValueType && !method.ContainsGenericParameters) { //FIXME find a way to build an invoke delegate for value types.
307 throw new ArgumentException ("Get Method not found for '" + Name + "'");
308 cached_getter = CreateGetterDelegate (method);
309 // The try-catch preserves the .Invoke () behaviour
311 return cached_getter (obj);
312 } catch (Exception ex) {
313 throw new TargetInvocationException (ex);
318 return cached_getter (obj);
319 } catch (Exception ex) {
320 throw new TargetInvocationException (ex);
326 return GetValue (obj, BindingFlags.Default, null, index, null);
329 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
333 MethodInfo method = GetGetMethod (true);
335 throw new ArgumentException ("Get Method not found for '" + Name + "'");
338 if (index == null || index.Length == 0)
339 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
341 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
343 catch (SecurityException se) {
344 throw new TargetInvocationException (se);
350 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
352 MethodInfo method = GetSetMethod (true);
354 throw new ArgumentException ("Set Method not found for '" + Name + "'");
357 if (index == null || index.Length == 0)
358 parms = new object [] {value};
360 int ilen = index.Length;
361 parms = new object [ilen+ 1];
362 index.CopyTo (parms, 0);
363 parms [ilen] = value;
366 method.Invoke (obj, invokeAttr, binder, parms, culture);
369 public override string ToString ()
371 var sb = new StringBuilder ();
373 Type retType = PropertyType;
374 if (Type.ShouldPrintFullName (retType))
375 sb.Append (retType.ToString ());
377 sb.Append (retType.Name);
382 var pi = GetIndexParameters ();
385 ParameterInfo.FormatParameters (sb, pi);
389 return sb.ToString ();
392 public override Type[] GetOptionalCustomModifiers () {
393 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, true);
395 return Type.EmptyTypes;
399 public override Type[] GetRequiredCustomModifiers () {
400 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, false);
402 return Type.EmptyTypes;
407 public void GetObjectData (SerializationInfo info, StreamingContext context)
409 MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
410 ToString(), MemberTypes.Property);
413 public override IList<CustomAttributeData> GetCustomAttributesData () {
414 return CustomAttributeData.GetCustomAttributes (this);