merge -r 58060:58217
[mono.git] / mcs / class / corlib / System.Reflection / Binder.cs
1 // System.Reflection.Binder
2 //
3 // Authors:
4 //      Sean MacIsaac (macisaac@ximian.com)
5 //      Paolo Molaro (lupus@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) Ximian, Inc. 2001 - 2003
9 // (c) Copyright 2004 Novell, Inc. (http://www.novell.com)
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Globalization;
35 using System.Runtime.InteropServices;
36
37 namespace System.Reflection
38 {
39 #if NET_2_0
40         [ComVisible (true)]
41 #endif
42         [Serializable]
43         [ClassInterface(ClassInterfaceType.AutoDual)]
44         public abstract class Binder
45         {
46                 protected Binder () {}
47
48                 public abstract FieldInfo BindToField (BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture);
49                 public abstract MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state);
50                 public abstract object ChangeType (object value, Type type, CultureInfo culture);
51                 public abstract void ReorderArgumentArray( ref object[] args, object state);
52                 public abstract MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers);
53                 public abstract PropertyInfo SelectProperty( BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers);
54
55                 static Binder default_binder = new Default ();
56
57                 internal static Binder DefaultBinder {
58                         get {
59                                 return default_binder;
60                         }
61                 }
62                 
63                 internal static bool ConvertArgs (Binder binder, object[] args, ParameterInfo[] pinfo, CultureInfo culture) {
64                         if (args == null) {
65                                 if ( pinfo.Length == 0)
66                                         return true;
67                                 else
68                                         throw new TargetParameterCountException ();
69                         }
70                         if (pinfo.Length != args.Length)
71                                 throw new TargetParameterCountException ();
72                         for (int i = 0; i < args.Length; ++i) {
73                                 object v = binder.ChangeType (args [i], pinfo[i].ParameterType, culture);
74                                 if ((v == null) && (args [i] != null))
75                                         return false;
76                                 args [i] = v;
77                         }
78                         return true;
79                 }
80
81                 internal static int GetDerivedLevel (Type type) 
82                 {
83                         Type searchType = type;
84                         int level = 1;
85
86                         while (searchType.BaseType != null) 
87                         {
88                                 level++;
89                                 searchType = searchType.BaseType;
90                         }
91
92                         return level;
93                 }
94
95                 internal static MethodBase FindMostDerivedMatch (MethodBase [] match) 
96                 {
97                         int highLevel = 0;
98                         int matchId = -1;
99                         int count = match.Length;
100
101                         for (int current = 0; current < count; current++) 
102                         {
103                                 int level = GetDerivedLevel (match[current].DeclaringType);
104                                 if (level == highLevel)
105                                         throw new AmbiguousMatchException ();
106
107                                 if (level > highLevel) 
108                                 {
109                                         highLevel = level;
110                                         matchId = current;
111                                 }
112                         }
113
114                         return match[matchId];
115                 }
116
117                 internal sealed class Default : Binder {
118                         public override FieldInfo BindToField (BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture) 
119                         {
120                                 if (match == null)
121                                         throw new ArgumentNullException ("match");
122                                 foreach (FieldInfo f in match) {
123                                         if (check_type (value.GetType (), f.FieldType))
124                                                 return f;
125                                 }
126                                 return null;
127                         }
128
129                         [MonoTODO]
130                         public override MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state)
131                         {
132                                 Type[] types;
133                                 if (args == null)
134                                         types = Type.EmptyTypes;
135                                 else {
136                                         types = new Type [args.Length];
137                                         for (int i = 0; i < args.Length; ++i) {
138                                                 if (args [i] != null)
139                                                         types [i] = args [i].GetType ();
140                                         }
141                                 }
142                                 MethodBase selected = SelectMethod (bindingAttr, match, types, modifiers);
143                                 state = null;
144                                 return selected;
145                         }
146
147                         static bool IsArrayAssignable (Type object_type, Type target_type)
148                         {
149                                 if (object_type.IsArray && target_type.IsArray)
150                                         return IsArrayAssignable (object_type.GetElementType (), target_type.GetElementType ());
151                                                 
152                                 if (target_type.IsAssignableFrom (object_type))
153                                         return true;
154
155                                 return false;
156                         }
157                         
158                         public override object ChangeType (object value, Type type, CultureInfo culture)
159                         {
160                                 if (value == null)
161                                         return null;
162                                 Type vtype = value.GetType ();
163                                 if (type.IsByRef)
164                                         type = type.GetElementType ();
165                                 if (vtype == type || type.IsInstanceOfType (value))
166                                         return value;
167                                 if (vtype.IsArray && type.IsArray){
168                                         if (IsArrayAssignable (vtype.GetElementType (), type.GetElementType ()))
169                                                 return value;
170                                 }
171
172                                 if (check_type (vtype, type))
173                                         return Convert.ChangeType (value, type);
174                                 return null;
175                         }
176
177                         [MonoTODO]
178                         public override void ReorderArgumentArray (ref object[] args, object state)
179                         {
180                                 //do nothing until we support named arguments
181                                 //throw new NotImplementedException ();
182                         }
183
184                         private static bool check_type (Type from, Type to) {
185                                 if (from == to)
186                                         return true;
187
188                                 if (from == null)
189                                         return true;
190
191                                 TypeCode fromt = Type.GetTypeCode (from);
192                                 TypeCode tot = Type.GetTypeCode (to);
193
194                                 if (to.IsByRef != from.IsByRef)
195                                         return false;
196
197                                 switch (fromt) {
198                                 case TypeCode.Char:
199                                         switch (tot) {
200                                         case TypeCode.UInt16:
201                                         case TypeCode.UInt32:
202                                         case TypeCode.Int32:
203                                         case TypeCode.UInt64:
204                                         case TypeCode.Int64:
205                                         case TypeCode.Single:
206                                         case TypeCode.Double:
207                                                 return true;
208                                         }
209                                         return to == typeof (object);
210                                 case TypeCode.Byte:
211                                         switch (tot) {
212                                         case TypeCode.Char:
213                                         case TypeCode.UInt16:
214                                         case TypeCode.Int16:
215                                         case TypeCode.UInt32:
216                                         case TypeCode.Int32:
217                                         case TypeCode.UInt64:
218                                         case TypeCode.Int64:
219                                         case TypeCode.Single:
220                                         case TypeCode.Double:
221                                                 return true;
222                                         }
223                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
224                                 case TypeCode.SByte:
225                                         switch (tot) {
226                                         case TypeCode.Int16:
227                                         case TypeCode.Int32:
228                                         case TypeCode.Int64:
229                                         case TypeCode.Single:
230                                         case TypeCode.Double:
231                                                 return true;
232                                         }
233                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
234                                 case TypeCode.UInt16:
235                                         switch (tot) {
236                                         case TypeCode.UInt32:
237                                         case TypeCode.Int32:
238                                         case TypeCode.UInt64:
239                                         case TypeCode.Int64:
240                                         case TypeCode.Single:
241                                         case TypeCode.Double:
242                                                 return true;
243                                         }
244                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
245                                 case TypeCode.Int16:
246                                         switch (tot) {
247                                         case TypeCode.Int32:
248                                         case TypeCode.Int64:
249                                         case TypeCode.Single:
250                                         case TypeCode.Double:
251                                                 return true;
252                                         }
253                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
254                                 case TypeCode.UInt32:
255                                         switch (tot) {
256                                         case TypeCode.UInt64:
257                                         case TypeCode.Int64:
258                                         case TypeCode.Single:
259                                         case TypeCode.Double:
260                                                 return true;
261                                         }
262                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
263                                 case TypeCode.Int32:
264                                         switch (tot) {
265                                         case TypeCode.Int64:
266                                         case TypeCode.Single:
267                                         case TypeCode.Double:
268                                                 return true;
269                                         }
270                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
271                                 case TypeCode.UInt64:
272                                 case TypeCode.Int64:
273                                         switch (tot) {
274                                         case TypeCode.Single:
275                                         case TypeCode.Double:
276                                                 return true;
277                                         }
278                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
279                                 case TypeCode.Single:
280                                         return tot == TypeCode.Double || to == typeof (object);
281                                 default:
282                                         /* TODO: handle valuetype -> byref */
283                                         if (to == typeof (object) && from.IsValueType)
284                                                 return true;
285
286                                         return to.IsAssignableFrom (from);
287                                 }
288                         }
289
290                         private static bool check_arguments (Type[] types, ParameterInfo[] args) {
291                                 for (int i = 0; i < types.Length; ++i) {
292                                         if (!check_type (types [i], args [i].ParameterType))
293                                                 return false;
294                                 }
295                                 return true;
296                         }
297
298                         public override MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
299                         {
300                                 MethodBase m;
301                                 int i, j;
302
303                                 if (match == null)
304                                         throw new ArgumentNullException ("match");
305
306                                 /* first look for an exact match... */
307                                 for (i = 0; i < match.Length; ++i) {
308                                         m = match [i];
309                                         ParameterInfo[] args = m.GetParameters ();
310                                         if (args.Length != types.Length)
311                                                 continue;
312                                         for (j = 0; j < types.Length; ++j) {
313                                                 if (types [j] != args [j].ParameterType)
314                                                         break;
315                                         }
316                                         if (j == types.Length)
317                                                 return m;
318                                 }
319
320                                 MethodBase result = null;
321                                 for (i = 0; i < match.Length; ++i) {
322                                         m = match [i];
323                                         ParameterInfo[] args = m.GetParameters ();
324                                         if (args.Length != types.Length)
325                                                 continue;
326                                         if (!check_arguments (types, args))
327                                                 continue;
328
329                                         if (result != null)
330                                                 throw new AmbiguousMatchException ();
331
332                                         result = m;
333                                 }
334
335                                 return result;
336                         }
337
338                         public override PropertyInfo SelectProperty (BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers)
339                         {
340                                 if (match == null || match.Length == 0)
341                                         throw new ArgumentException ("No properties provided", "match");
342
343                                 bool haveRet = (returnType != null);
344                                 int idxlen = (indexes != null) ? indexes.Length : -1;
345                                 PropertyInfo result = null;
346                                 int i;
347                                 int best_score = Int32.MaxValue - 1;
348                                 int fail_score = Int32.MaxValue;
349                                 int level = 0;
350                                 
351                                 for (i = match.Length - 1; i >= 0; i--) {
352                                         PropertyInfo p = match [i];
353                                         ParameterInfo[] args = p.GetIndexParameters ();
354                                         if (idxlen >= 0 && idxlen != args.Length)
355                                                 continue;
356
357                                         if (haveRet && !check_type (p.PropertyType, returnType))
358                                                 continue;
359
360                                         int score = Int32.MaxValue - 1;
361                                         if (idxlen > 0) {
362                                                 score = check_arguments_with_score (indexes, args);
363                                                 if (score == -1)
364                                                         continue;
365                                         }
366
367                                         int new_level = GetDerivedLevel (p.DeclaringType);
368                                         if (result != null) {
369                                                 if (best_score < score)
370                                                         continue;
371
372                                                 if (best_score == score) {
373                                                         if (level == new_level) {
374                                                                 // Keep searching. May be there's something
375                                                                 // better for us.
376                                                                 fail_score = score;
377                                                                 continue;
378                                                         }
379
380                                                         if (level > new_level)
381                                                                 continue;
382                                                 }
383                                         }
384
385                                         result = p;
386                                         best_score = score;
387                                         level = new_level;
388                                 }
389
390                                 if (fail_score <= best_score)
391                                         throw new AmbiguousMatchException ();
392
393                                 return result;
394                         }
395
396                         static int check_arguments_with_score (Type [] types, ParameterInfo [] args)
397                         {
398                                 int worst = -1;
399
400                                 for (int i = 0; i < types.Length; ++i) {
401                                         int res = check_type_with_score (types [i], args [i].ParameterType);
402                                         if (res == -1)
403                                                 return -1;
404
405                                         if (worst < res)
406                                                 worst = res;
407                                 }
408
409                                 return worst;
410                         }
411
412                         // 0 -> same type or null and !valuetype
413                         // 1 -> to == Enum
414                         // 2 -> value type that don't lose data
415                         // 3 -> to == IsAssignableFrom
416                         // 4 -> to == object
417                         static int check_type_with_score (Type from, Type to)
418                         {
419                                 if (from == null)
420                                         return to.IsValueType ? -1 : 0;
421
422                                 if (from == to)
423                                         return 0;
424
425                                 if (to == typeof (object))
426                                         return 4;
427
428                                 TypeCode fromt = Type.GetTypeCode (from);
429                                 TypeCode tot = Type.GetTypeCode (to);
430
431                                 switch (fromt) {
432                                 case TypeCode.Char:
433                                         switch (tot) {
434                                         case TypeCode.UInt16:
435                                                 return 0;
436
437                                         case TypeCode.UInt32:
438                                         case TypeCode.Int32:
439                                         case TypeCode.UInt64:
440                                         case TypeCode.Int64:
441                                         case TypeCode.Single:
442                                         case TypeCode.Double:
443                                                 return 2;
444                                         }
445                                         return -1;
446                                 case TypeCode.Byte:
447                                         switch (tot) {
448                                         case TypeCode.Char:
449                                         case TypeCode.UInt16:
450                                         case TypeCode.Int16:
451                                         case TypeCode.UInt32:
452                                         case TypeCode.Int32:
453                                         case TypeCode.UInt64:
454                                         case TypeCode.Int64:
455                                         case TypeCode.Single:
456                                         case TypeCode.Double:
457                                                 return 2;
458                                         }
459                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
460                                 case TypeCode.SByte:
461                                         switch (tot) {
462                                         case TypeCode.Int16:
463                                         case TypeCode.Int32:
464                                         case TypeCode.Int64:
465                                         case TypeCode.Single:
466                                         case TypeCode.Double:
467                                                 return 2;
468                                         }
469                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
470                                 case TypeCode.UInt16:
471                                         switch (tot) {
472                                         case TypeCode.UInt32:
473                                         case TypeCode.Int32:
474                                         case TypeCode.UInt64:
475                                         case TypeCode.Int64:
476                                         case TypeCode.Single:
477                                         case TypeCode.Double:
478                                                 return 2;
479                                         }
480                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
481                                 case TypeCode.Int16:
482                                         switch (tot) {
483                                         case TypeCode.Int32:
484                                         case TypeCode.Int64:
485                                         case TypeCode.Single:
486                                         case TypeCode.Double:
487                                                 return 2;
488                                         }
489                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
490                                 case TypeCode.UInt32:
491                                         switch (tot) {
492                                         case TypeCode.UInt64:
493                                         case TypeCode.Int64:
494                                         case TypeCode.Single:
495                                         case TypeCode.Double:
496                                                 return 2;
497                                         }
498                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
499                                 case TypeCode.Int32:
500                                         switch (tot) {
501                                         case TypeCode.Int64:
502                                         case TypeCode.Single:
503                                         case TypeCode.Double:
504                                                 return 2;
505                                         }
506                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
507                                 case TypeCode.UInt64:
508                                 case TypeCode.Int64:
509                                         switch (tot) {
510                                         case TypeCode.Single:
511                                         case TypeCode.Double:
512                                                 return 2;
513                                         }
514                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
515                                 case TypeCode.Single:
516                                         return tot == TypeCode.Double ? 2 : -1;
517                                 default:
518                                         return (to.IsAssignableFrom (from)) ? 3 : -1;
519                                 }
520                         }
521                 }
522         }
523 }
524