SqlBulkCopy Implementation
[mono.git] / mcs / class / corlib / System.Reflection / MonoProperty.cs
1 //
2 // MonoProperty.cs: The class used to represent Properties from the mono runtime.
3 //
4 // Authors:
5 //   Paolo Molaro (lupus@ximian.com)
6 //   Patrik Torstensson (patrik.torstensson@labs2.com)
7 //   Marek Safar (marek.safar@gmail.com)
8 //
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)
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
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;
39 using System.Text;
40
41 namespace System.Reflection {
42         
43         internal struct MonoPropertyInfo {
44                 public Type parent;
45                 public Type declaring_type;
46                 public String name;
47                 public MethodInfo get_method;
48                 public MethodInfo set_method;
49                 public PropertyAttributes attrs;
50                 
51                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52                 internal static extern void get_property_info (MonoProperty prop, ref MonoPropertyInfo info,
53                                                                PInfo req_info);
54
55                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
56                 internal static extern Type[] GetTypeModifiers (MonoProperty prop, bool optional);
57
58                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
59                 internal static extern object get_default_value (MonoProperty prop);
60         }
61
62         [Flags]
63         internal enum PInfo {
64                 Attributes = 1,
65                 GetMethod  = 1 << 1,
66                 SetMethod  = 1 << 2,
67                 ReflectedType = 1 << 3,
68                 DeclaringType = 1 << 4,
69                 Name = 1 << 5
70                 
71         }
72
73         internal delegate object GetterAdapter (object _this);
74         internal delegate R Getter<T,R> (T _this);
75
76         [Serializable]
77         [StructLayout (LayoutKind.Sequential)]
78         internal class MonoProperty : PropertyInfo, ISerializable {
79 #pragma warning disable 649
80                 internal IntPtr klass;
81                 internal IntPtr prop;
82                 MonoPropertyInfo info;
83                 PInfo cached;
84                 GetterAdapter cached_getter;
85
86 #pragma warning restore 649
87
88                 void CachePropertyInfo (PInfo flags)
89                 {
90                         if ((cached & flags) != flags) {
91                                 MonoPropertyInfo.get_property_info (this, ref info, flags);
92                                 cached |= flags;
93                         }
94                 }
95                 
96                 public override PropertyAttributes Attributes {
97                         get {
98                                 CachePropertyInfo (PInfo.Attributes);
99                                 return info.attrs;
100                         }
101                 }
102                 
103                 public override bool CanRead {
104                         get {
105                                 CachePropertyInfo (PInfo.GetMethod);
106                                 return (info.get_method != null);
107                         }
108                 }
109                 
110                 public override bool CanWrite {
111                         get {
112                                 CachePropertyInfo (PInfo.SetMethod);
113                                 return (info.set_method != null);
114                         }
115                 }
116
117                 public override Type PropertyType {
118                         get {
119                                 CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
120
121                                 if (info.get_method != null) {
122                                         return info.get_method.ReturnType;
123                                 } else {
124                                         ParameterInfo[] parameters = info.set_method.GetParametersInternal ();
125                                         
126                                         return parameters [parameters.Length - 1].ParameterType;
127                                 }
128                         }
129                 }
130
131                 public override Type ReflectedType {
132                         get {
133                                 CachePropertyInfo (PInfo.ReflectedType);
134                                 return info.parent;
135                         }
136                 }
137                 
138                 public override Type DeclaringType {
139                         get {
140                                 CachePropertyInfo (PInfo.DeclaringType);
141                                 return info.declaring_type;
142                         }
143                 }
144                 
145                 public override string Name {
146                         get {
147                                 CachePropertyInfo (PInfo.Name);
148                                 return info.name;
149                         }
150                 }
151
152                 public override MethodInfo[] GetAccessors (bool nonPublic)
153                 {
154                         int nget = 0;
155                         int nset = 0;
156                         
157                         CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
158
159                         if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
160                                 nset = 1;
161                         if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
162                                 nget = 1;
163
164                         MethodInfo[] res = new MethodInfo [nget + nset];
165                         int n = 0;
166                         if (nset != 0)
167                                 res [n++] = info.set_method;
168                         if (nget != 0)
169                                 res [n++] = info.get_method;
170                         return res;
171                 }
172
173                 public override MethodInfo GetGetMethod (bool nonPublic)
174                 {
175                         CachePropertyInfo (PInfo.GetMethod);
176                         if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
177                                 return info.get_method;
178                         else
179                                 return null;
180                 }
181
182                 public override ParameterInfo[] GetIndexParameters ()
183                 {
184                         CachePropertyInfo (PInfo.GetMethod | PInfo.SetMethod);
185                         ParameterInfo[] src;
186                         int length;
187                         if (info.get_method != null) {
188                                 src = info.get_method.GetParametersInternal ();
189                                 length = src.Length;
190                         } else if (info.set_method != null) {
191                                 src = info.set_method.GetParametersInternal ();
192                                 length = src.Length - 1;
193                         } else
194                                 return EmptyArray<ParameterInfo>.Value;
195
196                         var dest = new ParameterInfo [length];
197                         for (int i = 0; i < length; ++i) {
198                                 dest [i] = ParameterInfo.New (src [i], this);
199                         }
200                         return dest;    
201                 }
202                 
203                 public override MethodInfo GetSetMethod (bool nonPublic)
204                 {
205                         CachePropertyInfo (PInfo.SetMethod);
206                         if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
207                                 return info.set_method;
208                         else
209                                 return null;
210                 }
211
212
213                 /*TODO verify for attribute based default values, just like ParameterInfo*/
214                 public override object GetConstantValue ()
215                 {
216                         return MonoPropertyInfo.get_default_value (this);
217                 }
218
219                 public override object GetRawConstantValue() {
220                         return MonoPropertyInfo.get_default_value (this);
221                 }
222
223                 // According to MSDN the inherit parameter is ignored here and
224                 // the behavior always defaults to inherit = false
225                 //
226                 public override bool IsDefined (Type attributeType, bool inherit)
227                 {
228                         return MonoCustomAttrs.IsDefined (this, attributeType, false);
229                 }
230
231                 public override object[] GetCustomAttributes (bool inherit)
232                 {
233                         return MonoCustomAttrs.GetCustomAttributes (this, false);
234                 }
235                 
236                 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
237                 {
238                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, false);
239                 }
240
241
242                 delegate object GetterAdapter (object _this);
243                 delegate R Getter<T,R> (T _this);
244                 delegate R StaticGetter<R> ();
245
246 #pragma warning disable 169
247                 // Used via reflection
248                 static object GetterAdapterFrame<T,R> (Getter<T,R> getter, object obj)
249                 {
250                         return getter ((T)obj);
251                 }
252
253                 static object StaticGetterAdapterFrame<R> (StaticGetter<R> getter, object obj)
254                 {
255                         return getter ();
256                 }
257 #pragma warning restore 169
258
259                 /*
260                  * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
261                  * The first delegate cast the this argument to the right type and the second does points to the target method.
262                  */
263                 static GetterAdapter CreateGetterDelegate (MethodInfo method)
264                 {
265                         Type[] typeVector;
266                         Type getterType;
267                         object getterDelegate;
268                         MethodInfo adapterFrame;
269                         Type getterDelegateType;
270                         string frameName;
271
272                         if (method.IsStatic) {
273                                 typeVector = new Type[] { method.ReturnType };
274                                 getterDelegateType = typeof (StaticGetter<>);
275                                 frameName = "StaticGetterAdapterFrame";
276                         } else {
277                                 typeVector = new Type[] { method.DeclaringType, method.ReturnType };
278                                 getterDelegateType = typeof (Getter<,>);
279                                 frameName = "GetterAdapterFrame";
280                         }
281
282                         getterType = getterDelegateType.MakeGenericType (typeVector);
283 #if NET_2_1
284                         // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
285                         // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
286                         // delegate that we can transform into a MethodAccessException
287                         getterDelegate = Delegate.CreateDelegate (getterType, method, false);
288                         if (getterDelegate == null)
289                                 throw new MethodAccessException ();
290 #else
291                         getterDelegate = Delegate.CreateDelegate (getterType, method);
292 #endif
293                         adapterFrame = typeof (MonoProperty).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
294                         adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
295                         return (GetterAdapter)Delegate.CreateDelegate (typeof (GetterAdapter), getterDelegate, adapterFrame, true);
296                 }
297                         
298                 public override object GetValue (object obj, object[] index)
299                 {
300                         if (index == null || index.Length == 0) {
301                                 /*FIXME we should check if the number of arguments matches the expected one, otherwise the error message will be pretty criptic.*/
302 #if !MONOTOUCH
303                                 if (cached_getter == null) {
304                                         MethodInfo method = GetGetMethod (true);
305                                         if (!DeclaringType.IsValueType && !method.ContainsGenericParameters) { //FIXME find a way to build an invoke delegate for value types.
306                                                 if (method == null)
307                                                         throw new ArgumentException ("Get Method not found for '" + Name + "'");
308                                                 cached_getter = CreateGetterDelegate (method);
309                                                 // The try-catch preserves the .Invoke () behaviour
310                                                 try {
311                                                         return cached_getter (obj);
312                                                 } catch (Exception ex) {
313                                                         throw new TargetInvocationException (ex);
314                                                 }
315                                         }
316                                 } else {
317                                         try {
318                                                 return cached_getter (obj);
319                                         } catch (Exception ex) {
320                                                 throw new TargetInvocationException (ex);
321                                         }
322                                 }
323 #endif
324                         }
325
326                         return GetValue (obj, BindingFlags.Default, null, index, null);
327                 }
328
329                 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
330                 {
331                         object ret = null;
332
333                         MethodInfo method = GetGetMethod (true);
334                         if (method == null)
335                                 throw new ArgumentException ("Get Method not found for '" + Name + "'");
336
337                         try {
338                                 if (index == null || index.Length == 0) 
339                                         ret = method.Invoke (obj, invokeAttr, binder, null, culture);
340                                 else
341                                         ret = method.Invoke (obj, invokeAttr, binder, index, culture);
342                         }
343                         catch (SecurityException se) {
344                                 throw new TargetInvocationException (se);
345                         }
346
347                         return ret;
348                 }
349
350                 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
351                 {
352                         MethodInfo method = GetSetMethod (true);
353                         if (method == null)
354                                 throw new ArgumentException ("Set Method not found for '" + Name + "'");
355                         
356                         object [] parms;
357                         if (index == null || index.Length == 0) 
358                                 parms = new object [] {value};
359                         else {
360                                 int ilen = index.Length;
361                                 parms = new object [ilen+ 1];
362                                 index.CopyTo (parms, 0);
363                                 parms [ilen] = value;
364                         }
365
366                         method.Invoke (obj, invokeAttr, binder, parms, culture);
367                 }
368
369                 public override string ToString ()
370                 {
371                         var sb = new StringBuilder ();
372
373                         Type retType = PropertyType;
374                         if (Type.ShouldPrintFullName (retType))
375                                 sb.Append (retType.ToString ());
376                         else
377                                 sb.Append (retType.Name);
378
379                         sb.Append (" ");
380                         sb.Append (Name);
381
382                         var pi = GetIndexParameters ();
383                         if (pi.Length > 0) {
384                                 sb.Append (" [");
385                                 ParameterInfo.FormatParameters (sb, pi);
386                                 sb.Append ("]");
387                         }
388
389                         return sb.ToString ();
390                 }
391
392                 public override Type[] GetOptionalCustomModifiers () {
393                         Type[] types = MonoPropertyInfo.GetTypeModifiers (this, true);
394                         if (types == null)
395                                 return Type.EmptyTypes;
396                         return types;
397                 }
398
399                 public override Type[] GetRequiredCustomModifiers () {
400                         Type[] types = MonoPropertyInfo.GetTypeModifiers (this, false);
401                         if (types == null)
402                                 return Type.EmptyTypes;
403                         return types;
404                 }
405
406                 // ISerializable
407                 public void GetObjectData (SerializationInfo info, StreamingContext context) 
408                 {
409                         MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
410                                 ToString(), MemberTypes.Property);
411                 }
412
413 #if NET_4_0
414                 public override IList<CustomAttributeData> GetCustomAttributesData () {
415                         return CustomAttributeData.GetCustomAttributes (this);
416                 }
417 #endif
418         }
419 }