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 internal class MonoProperty : PropertyInfo, ISerializable {
76 #pragma warning disable 649
77 internal IntPtr klass;
79 MonoPropertyInfo info;
81 GetterAdapter cached_getter;
83 #pragma warning restore 649
85 void CachePropertyInfo (PInfo flags)
87 if ((cached & flags) != flags) {
88 MonoPropertyInfo.get_property_info (this, ref info, flags);
93 public override PropertyAttributes Attributes {
95 CachePropertyInfo (PInfo.Attributes);
100 public override bool CanRead {
102 CachePropertyInfo (PInfo.GetMethod);
103 return (info.get_method != null);
107 public override bool CanWrite {
109 CachePropertyInfo (PInfo.SetMethod);
110 return (info.set_method != null);
114 public override Type PropertyType {
116 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
118 if (info.get_method != null) {
119 return info.get_method.ReturnType;
121 ParameterInfo[] parameters = info.set_method.GetParameters ();
123 return parameters [parameters.Length - 1].ParameterType;
128 public override Type ReflectedType {
130 CachePropertyInfo (PInfo.ReflectedType);
135 public override Type DeclaringType {
137 CachePropertyInfo (PInfo.DeclaringType);
138 return info.declaring_type;
142 public override string Name {
144 CachePropertyInfo (PInfo.Name);
149 public override MethodInfo[] GetAccessors (bool nonPublic)
154 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
156 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
158 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
161 MethodInfo[] res = new MethodInfo [nget + nset];
164 res [n++] = info.set_method;
166 res [n++] = info.get_method;
170 public override MethodInfo GetGetMethod (bool nonPublic)
172 CachePropertyInfo (PInfo.GetMethod);
173 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
174 return info.get_method;
179 public override ParameterInfo[] GetIndexParameters()
181 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
183 if (info.get_method != null) {
184 res = info.get_method.GetParameters ();
185 } else if (info.set_method != null) {
186 ParameterInfo[] src = info.set_method.GetParameters ();
187 res = new ParameterInfo [src.Length - 1];
188 Array.Copy (src, res, res.Length);
190 return new ParameterInfo [0];
192 for (int i = 0; i < res.Length; ++i) {
193 ParameterInfo pinfo = res [i];
194 res [i] = new ParameterInfo (pinfo, this);
199 public override MethodInfo GetSetMethod (bool nonPublic)
201 CachePropertyInfo (PInfo.SetMethod);
202 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
203 return info.set_method;
209 /*TODO verify for attribute based default values, just like ParameterInfo*/
210 public override object GetConstantValue ()
212 return MonoPropertyInfo.get_default_value (this);
215 public override object GetRawConstantValue() {
216 return MonoPropertyInfo.get_default_value (this);
219 // According to MSDN the inherit parameter is ignored here and
220 // the behavior always defaults to inherit = false
222 public override bool IsDefined (Type attributeType, bool inherit)
224 return MonoCustomAttrs.IsDefined (this, attributeType, false);
227 public override object[] GetCustomAttributes (bool inherit)
229 return MonoCustomAttrs.GetCustomAttributes (this, false);
232 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
234 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, false);
238 delegate object GetterAdapter (object _this);
239 delegate R Getter<T,R> (T _this);
240 delegate R StaticGetter<R> ();
242 #pragma warning disable 169
243 // Used via reflection
244 static object GetterAdapterFrame<T,R> (Getter<T,R> getter, object obj)
246 return getter ((T)obj);
249 static object StaticGetterAdapterFrame<R> (StaticGetter<R> getter, object obj)
253 #pragma warning restore 169
256 * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
257 * The first delegate cast the this argument to the right type and the second does points to the target method.
259 static GetterAdapter CreateGetterDelegate (MethodInfo method)
263 object getterDelegate;
264 MethodInfo adapterFrame;
265 Type getterDelegateType;
268 if (method.IsStatic) {
269 typeVector = new Type[] { method.ReturnType };
270 getterDelegateType = typeof (StaticGetter<>);
271 frameName = "StaticGetterAdapterFrame";
273 typeVector = new Type[] { method.DeclaringType, method.ReturnType };
274 getterDelegateType = typeof (Getter<,>);
275 frameName = "GetterAdapterFrame";
278 getterType = getterDelegateType.MakeGenericType (typeVector);
280 // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
281 // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
282 // delegate that we can transform into a MethodAccessException
283 getterDelegate = Delegate.CreateDelegate (getterType, method, false);
284 if (getterDelegate == null)
285 throw new MethodAccessException ();
287 getterDelegate = Delegate.CreateDelegate (getterType, method);
289 adapterFrame = typeof (MonoProperty).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
290 adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
291 return (GetterAdapter)Delegate.CreateDelegate (typeof (GetterAdapter), getterDelegate, adapterFrame, true);
294 public override object GetValue (object obj, object[] index)
296 if (index == null || index.Length == 0) {
297 /*FIXME we should check if the number of arguments matches the expected one, otherwise the error message will be pretty criptic.*/
299 if (cached_getter == null) {
300 if (!DeclaringType.IsValueType) { //FIXME find a way to build an invoke delegate for value types.
301 MethodInfo method = GetGetMethod (true);
303 throw new ArgumentException ("Get Method not found for '" + Name + "'");
304 cached_getter = CreateGetterDelegate (method);
305 return cached_getter (obj);
308 return cached_getter (obj);
313 return GetValue (obj, BindingFlags.Default, null, index, null);
316 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
320 MethodInfo method = GetGetMethod (true);
322 throw new ArgumentException ("Get Method not found for '" + Name + "'");
325 if (index == null || index.Length == 0)
326 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
328 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
330 catch (SecurityException se) {
331 throw new TargetInvocationException (se);
337 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
339 MethodInfo method = GetSetMethod (true);
341 throw new ArgumentException ("Set Method not found for '" + Name + "'");
344 if (index == null || index.Length == 0)
345 parms = new object [] {value};
347 int ilen = index.Length;
348 parms = new object [ilen+ 1];
349 index.CopyTo (parms, 0);
350 parms [ilen] = value;
353 method.Invoke (obj, invokeAttr, binder, parms, culture);
356 public override string ToString () {
357 return PropertyType.ToString () + " " + Name;
360 public override Type[] GetOptionalCustomModifiers () {
361 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, true);
363 return Type.EmptyTypes;
367 public override Type[] GetRequiredCustomModifiers () {
368 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, false);
370 return Type.EmptyTypes;
375 public void GetObjectData (SerializationInfo info, StreamingContext context)
377 MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
378 ToString(), MemberTypes.Property);
382 public override IList<CustomAttributeData> GetCustomAttributesData () {
383 return CustomAttributeData.GetCustomAttributes (this);