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.Globalization;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.InteropServices;
35 using System.Runtime.Serialization;
36 using System.Security;
38 namespace System.Reflection {
40 internal struct MonoPropertyInfo {
43 public MethodInfo get_method;
44 public MethodInfo set_method;
45 public PropertyAttributes attrs;
47 [MethodImplAttribute(MethodImplOptions.InternalCall)]
48 internal static extern void get_property_info (MonoProperty prop, out MonoPropertyInfo info,
51 [MethodImplAttribute (MethodImplOptions.InternalCall)]
52 internal static extern Type[] GetTypeModifiers (MonoProperty prop, bool optional);
61 ReflectedType = 1 << 3,
62 DeclaringType = 1 << 4,
68 internal delegate object GetterAdapter (object _this);
69 internal delegate R Getter<T,R> (T _this);
73 internal class MonoProperty : PropertyInfo, ISerializable {
74 #pragma warning disable 649
75 internal IntPtr klass;
77 MonoPropertyInfo info;
80 GetterAdapter cached_getter;
83 #pragma warning restore 649
85 public override PropertyAttributes Attributes {
87 MonoPropertyInfo info;
88 MonoPropertyInfo.get_property_info (this, out info, PInfo.Attributes);
93 public override bool CanRead {
95 MonoPropertyInfo info;
96 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
97 return (info.get_method != null);
101 public override bool CanWrite {
103 MonoPropertyInfo info;
104 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
105 return (info.set_method != null);
109 public override Type PropertyType {
111 if ((cached & (PInfo.GetMethod | PInfo.SetMethod)) != (PInfo.GetMethod | PInfo.SetMethod)) {
112 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
113 cached |= (PInfo.GetMethod | PInfo.SetMethod);
116 if (info.get_method != null) {
117 return info.get_method.ReturnType;
119 ParameterInfo[] parameters = info.set_method.GetParameters ();
121 return parameters [parameters.Length - 1].ParameterType;
126 public override Type ReflectedType {
128 MonoPropertyInfo info;
129 MonoPropertyInfo.get_property_info (this, out info, PInfo.ReflectedType);
134 public override Type DeclaringType {
136 if ((cached & PInfo.DeclaringType) == 0) {
137 MonoPropertyInfo.get_property_info (this, out info, PInfo.DeclaringType);
138 cached |= PInfo.DeclaringType;
144 public override string Name {
146 if ((cached & PInfo.Name) == 0) {
147 MonoPropertyInfo.get_property_info (this, out info, PInfo.Name);
148 cached |= PInfo.Name;
154 public override MethodInfo[] GetAccessors (bool nonPublic)
156 MonoPropertyInfo info;
160 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
161 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
163 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
166 MethodInfo[] res = new MethodInfo [nget + nset];
169 res [n++] = info.set_method;
171 res [n++] = info.get_method;
175 public override MethodInfo GetGetMethod (bool nonPublic)
177 if ((cached & PInfo.GetMethod) == 0) {
178 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
179 cached |= PInfo.GetMethod;
181 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
182 return info.get_method;
187 public override ParameterInfo[] GetIndexParameters()
189 MonoPropertyInfo info;
190 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
191 if (info.get_method != null)
192 return info.get_method.GetParameters ();
193 return new ParameterInfo [0];
196 public override MethodInfo GetSetMethod (bool nonPublic)
198 if ((cached & PInfo.SetMethod) == 0) {
199 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
200 cached |= PInfo.SetMethod;
202 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
203 return info.set_method;
208 // According to MSDN the inherit parameter is ignored here and
209 // the behavior always defaults to inherit = false
211 public override bool IsDefined (Type attributeType, bool inherit)
213 return MonoCustomAttrs.IsDefined (this, attributeType, false);
216 public override object[] GetCustomAttributes (bool inherit)
218 return MonoCustomAttrs.GetCustomAttributes (this, false);
221 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
223 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, false);
228 delegate object GetterAdapter (object _this);
229 delegate R Getter<T,R> (T _this);
230 delegate R StaticGetter<R> ();
232 static object GetterAdapterFrame<T,R> (Getter<T,R> getter, object obj)
234 return getter ((T)obj);
237 static object StaticGetterAdapterFrame<R> (StaticGetter<R> getter, object obj)
243 * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
244 * The first delegate cast the this argument to the right type and the second does points to the target method.
246 static GetterAdapter CreateGetterDelegate (MethodInfo method)
250 object getterDelegate;
251 MethodInfo adapterFrame;
252 Type getterDelegateType;
255 if (method.IsStatic) {
256 typeVector = new Type[] { method.ReturnType };
257 getterDelegateType = typeof (StaticGetter<>);
258 frameName = "StaticGetterAdapterFrame";
260 typeVector = new Type[] { method.DeclaringType, method.ReturnType };
261 getterDelegateType = typeof (Getter<,>);
262 frameName = "GetterAdapterFrame";
265 getterType = getterDelegateType.MakeGenericType (typeVector);
267 // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
268 // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
269 // delegate that we can transform into a MethodAccessException
270 getterDelegate = Delegate.CreateDelegate (getterType, method, false);
271 if (getterDelegate == null)
272 throw new MethodAccessException ();
274 getterDelegate = Delegate.CreateDelegate (getterType, method);
276 adapterFrame = typeof (MonoProperty).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
277 adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
278 return (GetterAdapter)Delegate.CreateDelegate (typeof (GetterAdapter), getterDelegate, adapterFrame, true);
281 public override object GetValue (object obj, object[] index)
283 if (index == null || index.Length == 0) {
284 /*FIXME we should check if the number of arguments matches the expected one, otherwise the error message will be pretty criptic.*/
286 if (cached_getter == null) {
287 if (!DeclaringType.IsValueType) { //FIXME find a way to build an invoke delegate for value types.
288 MethodInfo method = GetGetMethod (true);
290 throw new ArgumentException ("Get Method not found for '" + Name + "'");
291 cached_getter = CreateGetterDelegate (method);
292 return cached_getter (obj);
295 return cached_getter (obj);
300 return GetValue (obj, BindingFlags.Default, null, index, null);
304 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
308 MethodInfo method = GetGetMethod (true);
310 throw new ArgumentException ("Get Method not found for '" + Name + "'");
313 if (index == null || index.Length == 0)
314 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
316 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
318 catch (SecurityException se) {
319 throw new TargetInvocationException (se);
325 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
327 MethodInfo method = GetSetMethod (true);
329 throw new ArgumentException ("Set Method not found for '" + Name + "'");
332 if (index == null || index.Length == 0)
333 parms = new object [] {value};
335 int ilen = index.Length;
336 parms = new object [ilen+ 1];
337 index.CopyTo (parms, 0);
338 parms [ilen] = value;
341 method.Invoke (obj, invokeAttr, binder, parms, culture);
344 public override string ToString () {
345 return PropertyType.ToString () + " " + Name;
348 #if NET_2_0 || BOOTSTRAP_NET_2_0
350 public override Type[] GetOptionalCustomModifiers () {
351 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, true);
353 return Type.EmptyTypes;
357 public override Type[] GetRequiredCustomModifiers () {
358 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, false);
360 return Type.EmptyTypes;
366 public void GetObjectData (SerializationInfo info, StreamingContext context)
368 MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
369 ToString(), MemberTypes.Property);