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;
40 using System.Diagnostics.Contracts;
42 namespace System.Reflection {
44 internal struct MonoPropertyInfo {
46 public Type declaring_type;
48 public MethodInfo get_method;
49 public MethodInfo set_method;
50 public PropertyAttributes attrs;
52 [MethodImplAttribute(MethodImplOptions.InternalCall)]
53 internal static extern void get_property_info (MonoProperty prop, ref MonoPropertyInfo info,
56 [MethodImplAttribute (MethodImplOptions.InternalCall)]
57 internal static extern Type[] GetTypeModifiers (MonoProperty prop, bool optional);
59 [MethodImplAttribute (MethodImplOptions.InternalCall)]
60 internal static extern object get_default_value (MonoProperty prop);
68 ReflectedType = 1 << 3,
69 DeclaringType = 1 << 4,
74 internal delegate object GetterAdapter (object _this);
75 internal delegate R Getter<T,R> (T _this);
77 abstract class RuntimePropertyInfo : PropertyInfo, ISerializable
79 internal BindingFlags BindingFlags {
85 public override Module Module {
87 return GetRuntimeModule ();
91 internal RuntimeType GetDeclaringTypeInternal ()
93 return (RuntimeType) DeclaringType;
96 RuntimeType ReflectedTypeInternal {
98 return (RuntimeType) ReflectedType;
102 internal RuntimeModule GetRuntimeModule ()
104 return GetDeclaringTypeInternal ().GetRuntimeModule ();
107 #region Object Overrides
108 public override String ToString()
110 return FormatNameAndSig(false);
113 private string FormatNameAndSig(bool serialization)
115 StringBuilder sbName = new StringBuilder(PropertyType.FormatTypeName(serialization));
120 var pi = GetIndexParameters ();
122 sbName.Append (" [");
123 ParameterInfo.FormatParameters (sbName, pi, 0, serialization);
127 return sbName.ToString();
131 #region ISerializable Implementation
132 public void GetObjectData(SerializationInfo info, StreamingContext context)
135 throw new ArgumentNullException("info");
136 Contract.EndContractBlock();
138 MemberInfoSerializationHolder.GetSerializationInfo(
141 ReflectedTypeInternal,
143 SerializationToString(),
144 MemberTypes.Property,
148 internal string SerializationToString()
150 return FormatNameAndSig(true);
156 [StructLayout (LayoutKind.Sequential)]
157 internal class MonoProperty : RuntimePropertyInfo {
158 #pragma warning disable 649
159 internal IntPtr klass;
160 internal IntPtr prop;
161 MonoPropertyInfo info;
163 GetterAdapter cached_getter;
165 #pragma warning restore 649
167 void CachePropertyInfo (PInfo flags)
169 if ((cached & flags) != flags) {
170 MonoPropertyInfo.get_property_info (this, ref info, flags);
175 public override PropertyAttributes Attributes {
177 CachePropertyInfo (PInfo.Attributes);
182 public override bool CanRead {
184 CachePropertyInfo (PInfo.GetMethod);
185 return (info.get_method != null);
189 public override bool CanWrite {
191 CachePropertyInfo (PInfo.SetMethod);
192 return (info.set_method != null);
196 public override Type PropertyType {
198 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
200 if (info.get_method != null) {
201 return info.get_method.ReturnType;
203 ParameterInfo[] parameters = info.set_method.GetParametersInternal ();
205 return parameters [parameters.Length - 1].ParameterType;
210 public override Type ReflectedType {
212 CachePropertyInfo (PInfo.ReflectedType);
217 public override Type DeclaringType {
219 CachePropertyInfo (PInfo.DeclaringType);
220 return info.declaring_type;
224 public override string Name {
226 CachePropertyInfo (PInfo.Name);
231 public override MethodInfo[] GetAccessors (bool nonPublic)
236 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
238 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
240 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
243 MethodInfo[] res = new MethodInfo [nget + nset];
246 res [n++] = info.set_method;
248 res [n++] = info.get_method;
252 public override MethodInfo GetGetMethod (bool nonPublic)
254 CachePropertyInfo (PInfo.GetMethod);
255 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
256 return info.get_method;
261 public override ParameterInfo[] GetIndexParameters ()
263 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
266 if (info.get_method != null) {
267 src = info.get_method.GetParametersInternal ();
269 } else if (info.set_method != null) {
270 src = info.set_method.GetParametersInternal ();
271 length = src.Length - 1;
273 return EmptyArray<ParameterInfo>.Value;
275 var dest = new ParameterInfo [length];
276 for (int i = 0; i < length; ++i) {
277 dest [i] = ParameterInfo.New (src [i], this);
282 public override MethodInfo GetSetMethod (bool nonPublic)
284 CachePropertyInfo (PInfo.SetMethod);
285 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
286 return info.set_method;
292 /*TODO verify for attribute based default values, just like ParameterInfo*/
293 public override object GetConstantValue ()
295 return MonoPropertyInfo.get_default_value (this);
298 public override object GetRawConstantValue() {
299 return MonoPropertyInfo.get_default_value (this);
302 // According to MSDN the inherit parameter is ignored here and
303 // the behavior always defaults to inherit = false
305 public override bool IsDefined (Type attributeType, bool inherit)
307 return MonoCustomAttrs.IsDefined (this, attributeType, false);
310 public override object[] GetCustomAttributes (bool inherit)
312 return MonoCustomAttrs.GetCustomAttributes (this, false);
315 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
317 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, false);
321 delegate object GetterAdapter (object _this);
322 delegate R Getter<T,R> (T _this);
323 delegate R StaticGetter<R> ();
325 #pragma warning disable 169
326 // Used via reflection
327 static object GetterAdapterFrame<T,R> (Getter<T,R> getter, object obj)
329 return getter ((T)obj);
332 static object StaticGetterAdapterFrame<R> (StaticGetter<R> getter, object obj)
336 #pragma warning restore 169
339 * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
340 * The first delegate cast the this argument to the right type and the second does points to the target method.
342 static GetterAdapter CreateGetterDelegate (MethodInfo method)
346 object getterDelegate;
347 MethodInfo adapterFrame;
348 Type getterDelegateType;
351 if (method.IsStatic) {
352 typeVector = new Type[] { method.ReturnType };
353 getterDelegateType = typeof (StaticGetter<>);
354 frameName = "StaticGetterAdapterFrame";
356 typeVector = new Type[] { method.DeclaringType, method.ReturnType };
357 getterDelegateType = typeof (Getter<,>);
358 frameName = "GetterAdapterFrame";
361 getterType = getterDelegateType.MakeGenericType (typeVector);
363 // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
364 // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
365 // delegate that we can transform into a MethodAccessException
366 getterDelegate = Delegate.CreateDelegate (getterType, method, false);
367 if (getterDelegate == null)
368 throw new MethodAccessException ();
370 getterDelegate = Delegate.CreateDelegate (getterType, method);
372 adapterFrame = typeof (MonoProperty).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
373 adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
374 return (GetterAdapter)Delegate.CreateDelegate (typeof (GetterAdapter), getterDelegate, adapterFrame, true);
377 public override object GetValue (object obj, object[] index)
379 if (index == null || index.Length == 0) {
380 /*FIXME we should check if the number of arguments matches the expected one, otherwise the error message will be pretty criptic.*/
381 #if !FULL_AOT_RUNTIME
382 if (cached_getter == null) {
383 MethodInfo method = GetGetMethod (true);
384 if (!DeclaringType.IsValueType && !method.ContainsGenericParameters) { //FIXME find a way to build an invoke delegate for value types.
386 throw new ArgumentException ("Get Method not found for '" + Name + "'");
387 cached_getter = CreateGetterDelegate (method);
388 // The try-catch preserves the .Invoke () behaviour
390 return cached_getter (obj);
391 } catch (Exception ex) {
392 throw new TargetInvocationException (ex);
397 return cached_getter (obj);
398 } catch (Exception ex) {
399 throw new TargetInvocationException (ex);
405 return GetValue (obj, BindingFlags.Default, null, index, null);
408 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
412 MethodInfo method = GetGetMethod (true);
414 throw new ArgumentException ("Get Method not found for '" + Name + "'");
417 if (index == null || index.Length == 0)
418 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
420 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
422 catch (SecurityException se) {
423 throw new TargetInvocationException (se);
429 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
431 MethodInfo method = GetSetMethod (true);
433 throw new ArgumentException ("Set Method not found for '" + Name + "'");
436 if (index == null || index.Length == 0)
437 parms = new object [] {value};
439 int ilen = index.Length;
440 parms = new object [ilen+ 1];
441 index.CopyTo (parms, 0);
442 parms [ilen] = value;
445 method.Invoke (obj, invokeAttr, binder, parms, culture);
448 public override Type[] GetOptionalCustomModifiers () {
449 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, true);
451 return Type.EmptyTypes;
455 public override Type[] GetRequiredCustomModifiers () {
456 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, false);
458 return Type.EmptyTypes;
462 public override IList<CustomAttributeData> GetCustomAttributesData () {
463 return CustomAttributeData.GetCustomAttributes (this);