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, ref 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 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);
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);
182 if (info.get_method != null)
183 return info.get_method.GetParameters ();
184 return new ParameterInfo [0];
187 public override MethodInfo GetSetMethod (bool nonPublic)
189 CachePropertyInfo (PInfo.SetMethod);
190 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
191 return info.set_method;
196 // According to MSDN the inherit parameter is ignored here and
197 // the behavior always defaults to inherit = false
199 public override bool IsDefined (Type attributeType, bool inherit)
201 return MonoCustomAttrs.IsDefined (this, attributeType, false);
204 public override object[] GetCustomAttributes (bool inherit)
206 return MonoCustomAttrs.GetCustomAttributes (this, false);
209 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
211 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, false);
216 delegate object GetterAdapter (object _this);
217 delegate R Getter<T,R> (T _this);
218 delegate R StaticGetter<R> ();
220 static object GetterAdapterFrame<T,R> (Getter<T,R> getter, object obj)
222 return getter ((T)obj);
225 static object StaticGetterAdapterFrame<R> (StaticGetter<R> getter, object obj)
231 * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
232 * The first delegate cast the this argument to the right type and the second does points to the target method.
234 static GetterAdapter CreateGetterDelegate (MethodInfo method)
238 object getterDelegate;
239 MethodInfo adapterFrame;
240 Type getterDelegateType;
243 if (method.IsStatic) {
244 typeVector = new Type[] { method.ReturnType };
245 getterDelegateType = typeof (StaticGetter<>);
246 frameName = "StaticGetterAdapterFrame";
248 typeVector = new Type[] { method.DeclaringType, method.ReturnType };
249 getterDelegateType = typeof (Getter<,>);
250 frameName = "GetterAdapterFrame";
253 getterType = getterDelegateType.MakeGenericType (typeVector);
255 // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
256 // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
257 // delegate that we can transform into a MethodAccessException
258 getterDelegate = Delegate.CreateDelegate (getterType, method, false);
259 if (getterDelegate == null)
260 throw new MethodAccessException ();
262 getterDelegate = Delegate.CreateDelegate (getterType, method);
264 adapterFrame = typeof (MonoProperty).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
265 adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
266 return (GetterAdapter)Delegate.CreateDelegate (typeof (GetterAdapter), getterDelegate, adapterFrame, true);
269 public override object GetValue (object obj, object[] index)
271 if (index == null || index.Length == 0) {
272 /*FIXME we should check if the number of arguments matches the expected one, otherwise the error message will be pretty criptic.*/
274 if (cached_getter == null) {
275 if (!DeclaringType.IsValueType) { //FIXME find a way to build an invoke delegate for value types.
276 MethodInfo method = GetGetMethod (true);
278 throw new ArgumentException ("Get Method not found for '" + Name + "'");
279 cached_getter = CreateGetterDelegate (method);
280 return cached_getter (obj);
283 return cached_getter (obj);
288 return GetValue (obj, BindingFlags.Default, null, index, null);
292 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
296 MethodInfo method = GetGetMethod (true);
298 throw new ArgumentException ("Get Method not found for '" + Name + "'");
301 if (index == null || index.Length == 0)
302 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
304 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
306 catch (SecurityException se) {
307 throw new TargetInvocationException (se);
313 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
315 MethodInfo method = GetSetMethod (true);
317 throw new ArgumentException ("Set Method not found for '" + Name + "'");
320 if (index == null || index.Length == 0)
321 parms = new object [] {value};
323 int ilen = index.Length;
324 parms = new object [ilen+ 1];
325 index.CopyTo (parms, 0);
326 parms [ilen] = value;
329 method.Invoke (obj, invokeAttr, binder, parms, culture);
332 public override string ToString () {
333 return PropertyType.ToString () + " " + Name;
336 #if NET_2_0 || BOOTSTRAP_NET_2_0
338 public override Type[] GetOptionalCustomModifiers () {
339 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, true);
341 return Type.EmptyTypes;
345 public override Type[] GetRequiredCustomModifiers () {
346 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, false);
348 return Type.EmptyTypes;
354 public void GetObjectData (SerializationInfo info, StreamingContext context)
356 MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
357 ToString(), MemberTypes.Property);