2 // System.Reflection/MonoProperty.cs
3 // The class used to represent Properties from the mono runtime.
6 // Paolo Molaro (lupus@ximian.com)
7 // Patrik Torstensson (patrik.torstensson@labs2.com)
9 // (C) 2001 Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections.Generic;
33 using System.Globalization;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Runtime.Serialization;
37 using System.Security;
39 namespace System.Reflection {
41 internal struct MonoPropertyInfo {
43 public Type declaring_type;
45 public MethodInfo get_method;
46 public MethodInfo set_method;
47 public PropertyAttributes attrs;
49 [MethodImplAttribute(MethodImplOptions.InternalCall)]
50 internal static extern void get_property_info (MonoProperty prop, ref MonoPropertyInfo info,
53 [MethodImplAttribute (MethodImplOptions.InternalCall)]
54 internal static extern Type[] GetTypeModifiers (MonoProperty prop, bool optional);
56 [MethodImplAttribute (MethodImplOptions.InternalCall)]
57 internal static extern object get_default_value (MonoProperty prop);
65 ReflectedType = 1 << 3,
66 DeclaringType = 1 << 4,
71 internal delegate object GetterAdapter (object _this);
72 internal delegate R Getter<T,R> (T _this);
75 [StructLayout (LayoutKind.Sequential)]
76 internal class MonoProperty : PropertyInfo, ISerializable {
77 #pragma warning disable 649
78 internal IntPtr klass;
80 MonoPropertyInfo info;
82 GetterAdapter cached_getter;
84 #pragma warning restore 649
86 void CachePropertyInfo (PInfo flags)
88 if ((cached & flags) != flags) {
89 MonoPropertyInfo.get_property_info (this, ref info, flags);
94 public override PropertyAttributes Attributes {
96 CachePropertyInfo (PInfo.Attributes);
101 public override bool CanRead {
103 CachePropertyInfo (PInfo.GetMethod);
104 return (info.get_method != null);
108 public override bool CanWrite {
110 CachePropertyInfo (PInfo.SetMethod);
111 return (info.set_method != null);
115 public override Type PropertyType {
117 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
119 if (info.get_method != null) {
120 return info.get_method.ReturnType;
122 ParameterInfo[] parameters = info.set_method.GetParametersInternal ();
124 return parameters [parameters.Length - 1].ParameterType;
129 public override Type ReflectedType {
131 CachePropertyInfo (PInfo.ReflectedType);
136 public override Type DeclaringType {
138 CachePropertyInfo (PInfo.DeclaringType);
139 return info.declaring_type;
143 public override string Name {
145 CachePropertyInfo (PInfo.Name);
150 public override MethodInfo[] GetAccessors (bool nonPublic)
155 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
157 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
159 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
162 MethodInfo[] res = new MethodInfo [nget + nset];
165 res [n++] = info.set_method;
167 res [n++] = info.get_method;
171 public override MethodInfo GetGetMethod (bool nonPublic)
173 CachePropertyInfo (PInfo.GetMethod);
174 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
175 return info.get_method;
180 public override ParameterInfo[] GetIndexParameters()
182 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
184 if (info.get_method != null) {
185 res = info.get_method.GetParameters ();
186 } else if (info.set_method != null) {
187 ParameterInfo[] src = info.set_method.GetParametersInternal ();
188 res = new ParameterInfo [src.Length - 1];
189 Array.Copy (src, res, res.Length);
191 return EmptyArray<ParameterInfo>.Value;
193 for (int i = 0; i < res.Length; ++i) {
194 ParameterInfo pinfo = res [i];
195 res [i] = new ParameterInfo (pinfo, this);
200 public override MethodInfo GetSetMethod (bool nonPublic)
202 CachePropertyInfo (PInfo.SetMethod);
203 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
204 return info.set_method;
210 /*TODO verify for attribute based default values, just like ParameterInfo*/
211 public override object GetConstantValue ()
213 return MonoPropertyInfo.get_default_value (this);
216 public override object GetRawConstantValue() {
217 return MonoPropertyInfo.get_default_value (this);
220 // According to MSDN the inherit parameter is ignored here and
221 // the behavior always defaults to inherit = false
223 public override bool IsDefined (Type attributeType, bool inherit)
225 return MonoCustomAttrs.IsDefined (this, attributeType, false);
228 public override object[] GetCustomAttributes (bool inherit)
230 return MonoCustomAttrs.GetCustomAttributes (this, false);
233 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
235 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, false);
239 delegate object GetterAdapter (object _this);
240 delegate R Getter<T,R> (T _this);
241 delegate R StaticGetter<R> ();
243 #pragma warning disable 169
244 // Used via reflection
245 static object GetterAdapterFrame<T,R> (Getter<T,R> getter, object obj)
247 return getter ((T)obj);
250 static object StaticGetterAdapterFrame<R> (StaticGetter<R> getter, object obj)
254 #pragma warning restore 169
257 * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
258 * The first delegate cast the this argument to the right type and the second does points to the target method.
260 static GetterAdapter CreateGetterDelegate (MethodInfo method)
264 object getterDelegate;
265 MethodInfo adapterFrame;
266 Type getterDelegateType;
269 if (method.IsStatic) {
270 typeVector = new Type[] { method.ReturnType };
271 getterDelegateType = typeof (StaticGetter<>);
272 frameName = "StaticGetterAdapterFrame";
274 typeVector = new Type[] { method.DeclaringType, method.ReturnType };
275 getterDelegateType = typeof (Getter<,>);
276 frameName = "GetterAdapterFrame";
279 getterType = getterDelegateType.MakeGenericType (typeVector);
281 // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
282 // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
283 // delegate that we can transform into a MethodAccessException
284 getterDelegate = Delegate.CreateDelegate (getterType, method, false);
285 if (getterDelegate == null)
286 throw new MethodAccessException ();
288 getterDelegate = Delegate.CreateDelegate (getterType, method);
290 adapterFrame = typeof (MonoProperty).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
291 adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
292 return (GetterAdapter)Delegate.CreateDelegate (typeof (GetterAdapter), getterDelegate, adapterFrame, true);
295 public override object GetValue (object obj, object[] index)
297 if (index == null || index.Length == 0) {
298 /*FIXME we should check if the number of arguments matches the expected one, otherwise the error message will be pretty criptic.*/
300 if (cached_getter == null) {
301 MethodInfo method = GetGetMethod (true);
302 if (!DeclaringType.IsValueType && !method.ContainsGenericParameters) { //FIXME find a way to build an invoke delegate for value types.
304 throw new ArgumentException ("Get Method not found for '" + Name + "'");
305 cached_getter = CreateGetterDelegate (method);
306 // The try-catch preserves the .Invoke () behaviour
308 return cached_getter (obj);
309 } catch (Exception ex) {
310 throw new TargetInvocationException (ex);
315 return cached_getter (obj);
316 } catch (Exception ex) {
317 throw new TargetInvocationException (ex);
323 return GetValue (obj, BindingFlags.Default, null, index, null);
326 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
330 MethodInfo method = GetGetMethod (true);
332 throw new ArgumentException ("Get Method not found for '" + Name + "'");
335 if (index == null || index.Length == 0)
336 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
338 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
340 catch (SecurityException se) {
341 throw new TargetInvocationException (se);
347 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
349 MethodInfo method = GetSetMethod (true);
351 throw new ArgumentException ("Set Method not found for '" + Name + "'");
354 if (index == null || index.Length == 0)
355 parms = new object [] {value};
357 int ilen = index.Length;
358 parms = new object [ilen+ 1];
359 index.CopyTo (parms, 0);
360 parms [ilen] = value;
363 method.Invoke (obj, invokeAttr, binder, parms, culture);
366 public override string ToString () {
367 return PropertyType.ToString () + " " + Name;
370 public override Type[] GetOptionalCustomModifiers () {
371 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, true);
373 return Type.EmptyTypes;
377 public override Type[] GetRequiredCustomModifiers () {
378 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, false);
380 return Type.EmptyTypes;
385 public void GetObjectData (SerializationInfo info, StreamingContext context)
387 MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
388 ToString(), MemberTypes.Property);
392 public override IList<CustomAttributeData> GetCustomAttributesData () {
393 return CustomAttributeData.GetCustomAttributes (this);