namespace System.Reflection
{
-#if NET_2_0
[ComVisible (true)]
-#endif
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDual)]
public abstract class Binder
for (int current = 0; current < count; current++)
{
- int level = GetDerivedLevel (match[current].DeclaringType);
+ MethodBase m = match [current];
+ int level = GetDerivedLevel (m.DeclaringType);
if (level == highLevel)
throw new AmbiguousMatchException ();
+ // If the argument types differ we
+ // have an ambigous match, as well
+ if (matchId >= 0) {
+ ParameterInfo[] p1 = m.GetParameters ();
+ ParameterInfo[] p2 = match [matchId].GetParameters ();
+ bool equal = true;
+
+ if (p1.Length != p2.Length)
+ equal = false;
+ else {
+ int i;
+
+ for (i = 0; i < p1.Length; ++i) {
+ if (p1 [i].ParameterType != p2 [i].ParameterType) {
+ equal = false;
+ break;
+ }
+ }
+ }
+
+ if (!equal)
+ throw new AmbiguousMatchException ();
+ }
if (level > highLevel)
{
return null;
}
- [MonoTODO]
+ //
+ // FIXME: There was a MonoTODO, but it does not explain what the problem is
+ //
public override MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state)
{
Type[] types;
types [i] = args [i].GetType ();
}
}
- MethodBase selected = SelectMethod (bindingAttr, match, types, modifiers);
+ MethodBase selected = SelectMethod (bindingAttr, match, types, modifiers, true);
state = null;
+ if (names != null)
+ ReorderParameters (names, ref args, selected);
+
+ if (selected != null) {
+ if (args == null)
+ args = new object [0];
+
+ AdjustArguments (selected, ref args);
+ }
+
return selected;
}
+ // probably belongs in ReorderArgumentArray
+ static void AdjustArguments (MethodBase selected, ref object [] args)
+ {
+ var parameters = selected.GetParameters ();
+ if (parameters.Length == 0)
+ return;
+
+ var last_parameter = parameters [parameters.Length - 1];
+ if (!Attribute.IsDefined (last_parameter, typeof (ParamArrayAttribute)))
+ return;
+
+ var adjusted = new object [parameters.Length];
+ Array.Copy (args, adjusted, parameters.Length - 1);
+
+ var param_args_count = args.Length + 1 - parameters.Length;
+ var params_args = Array.CreateInstance (last_parameter.ParameterType.GetElementType (), param_args_count);
+
+ for (int i = 0; i < param_args_count; i++)
+ params_args.SetValue (args [args.Length - param_args_count + i], i);
+
+ adjusted [adjusted.Length - 1] = params_args;
+ args = adjusted;
+ }
+
+ void ReorderParameters (string [] names, ref object [] args, MethodBase selected)
+ {
+ object [] newArgs = new object [args.Length];
+ Array.Copy (args, newArgs, args.Length);
+ ParameterInfo [] plist = selected.GetParameters ();
+ for (int n = 0; n < names.Length; n++)
+ for (int p = 0; p < plist.Length; p++) {
+ if (names [n] == plist [p].Name) {
+ newArgs [p] = args [n];
+ break;
+ }
+ }
+ Array.Copy (newArgs, args, args.Length);
+ }
+
static bool IsArrayAssignable (Type object_type, Type target_type)
{
if (object_type.IsArray && target_type.IsArray)
return value;
}
- if (check_type (vtype, type))
+ if (check_type (vtype, type)) {
+ // These are not supported by Convert
+ if (type.IsEnum)
+ return Enum.ToObject (type, value);
+ if (vtype == typeof (Char)) {
+ if (type == typeof (double))
+ return (double)(char)value;
+ if (type == typeof (float))
+ return (float)(char)value;
+ }
+ if (vtype == typeof (IntPtr) && type.IsPointer)
+ return value;
return Convert.ChangeType (value, type);
+ }
return null;
}
- [MonoTODO]
+ [MonoTODO ("This method does not do anything in Mono")]
public override void ReorderArgumentArray (ref object[] args, object state)
{
//do nothing until we support named arguments
return true;
if (from == null)
- return !to.IsValueType;
-
- TypeCode fromt = Type.GetTypeCode (from);
- TypeCode tot = Type.GetTypeCode (to);
+ return true;
if (to.IsByRef != from.IsByRef)
return false;
+ if (to.IsInterface)
+ return to.IsAssignableFrom (from);
+
+ if (to.IsEnum) {
+ to = Enum.GetUnderlyingType (to);
+ if (from == to)
+ return true;
+ }
+
+ if (to.IsGenericType && to.GetGenericTypeDefinition () == typeof (Nullable<>) && to.GetGenericArguments ()[0] == from)
+ return true;
+
+ TypeCode fromt = Type.GetTypeCode (from);
+ TypeCode tot = Type.GetTypeCode (to);
+
switch (fromt) {
case TypeCode.Char:
switch (tot) {
/* TODO: handle valuetype -> byref */
if (to == typeof (object) && from.IsValueType)
return true;
+ if (to.IsPointer && from == typeof (IntPtr))
+ return true;
return to.IsAssignableFrom (from);
}
}
- private static bool check_arguments (Type[] types, ParameterInfo[] args) {
+ private static bool check_arguments (Type[] types, ParameterInfo[] args, bool allowByRefMatch) {
for (int i = 0; i < types.Length; ++i) {
- if (!check_type (types [i], args [i].ParameterType))
+ bool match = check_type (types [i], args [i].ParameterType);
+ if (!match && allowByRefMatch) {
+ Type param_type = args [i].ParameterType;
+ if (param_type.IsByRef)
+ match = check_type (types [i], param_type.GetElementType ());
+ }
+ if (!match)
return false;
}
return true;
}
- public override MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
+ public override MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase [] match, Type [] types, ParameterModifier [] modifiers)
+ {
+ return SelectMethod (bindingAttr, match, types, modifiers,
+ false);
+ }
+
+ MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers, bool allowByRefMatch)
{
MethodBase m;
int i, j;
throw new ArgumentNullException ("match");
/* first look for an exact match... */
+ MethodBase exact_match = null;
for (i = 0; i < match.Length; ++i) {
m = match [i];
ParameterInfo[] args = m.GetParameters ();
if (types [j] != args [j].ParameterType)
break;
}
+ if (j == types.Length) {
+ if (exact_match != null) {
+ exact_match = null;
+ break;
+ } else {
+ exact_match = m;
+ }
+ }
+ }
+ if (exact_match != null)
+ return exact_match;
+
+ /* Try methods with ParamArray attribute */
+ bool isdefParamArray = false;
+ Type elementType = null;
+ for (i = 0; i < match.Length; ++i) {
+ m = match [i];
+ ParameterInfo[] args = m.GetParameters ();
+ if (args.Length > types.Length + 1)
+ continue;
+ else if (args.Length == 0)
+ continue;
+ isdefParamArray = Attribute.IsDefined (args [args.Length - 1], typeof (ParamArrayAttribute));
+ if (!isdefParamArray)
+ continue;
+ elementType = args [args.Length - 1].ParameterType.GetElementType ();
+ for (j = 0; j < types.Length; ++j) {
+ if (j < (args.Length - 1) && types [j] != args [j].ParameterType)
+ break;
+ else if (j >= (args.Length - 1) && types [j] != elementType)
+ break;
+ }
if (j == types.Length)
return m;
}
+ if ((int)(bindingAttr & BindingFlags.ExactBinding) != 0)
+ return null;
+
MethodBase result = null;
for (i = 0; i < match.Length; ++i) {
m = match [i];
ParameterInfo[] args = m.GetParameters ();
if (args.Length != types.Length)
continue;
- if (!check_arguments (types, args))
+ if (!check_arguments (types, args, allowByRefMatch))
continue;
if (result != null)
- throw new AmbiguousMatchException ();
-
- result = m;
+ result = GetBetterMethod (result, m, types);
+ else
+ result = m;
}
return result;
}
+ MethodBase GetBetterMethod (MethodBase m1, MethodBase m2, Type [] types)
+ {
+ ParameterInfo [] pl1 = m1.GetParameters ();
+ ParameterInfo [] pl2 = m2.GetParameters ();
+ int prev = 0;
+ for (int i = 0; i < pl1.Length; i++) {
+ int cmp = CompareCloserType (pl1 [i].ParameterType, pl2 [i].ParameterType);
+ if (cmp != 0 && prev != 0 && prev != cmp)
+ throw new AmbiguousMatchException ();
+ if (cmp != 0)
+ prev = cmp;
+ }
+ if (prev != 0)
+ return prev > 0 ? m2 : m1;
+
+ Type dt1 = m1.DeclaringType;
+ Type dt2 = m2.DeclaringType;
+ if (dt1 != dt2) {
+ if (dt1.IsSubclassOf(dt2))
+ return m1;
+ if (dt2.IsSubclassOf(dt1))
+ return m2;
+ }
+
+ bool va1 = (m1.CallingConvention & CallingConventions.VarArgs) != 0;
+ bool va2 = (m2.CallingConvention & CallingConventions.VarArgs) != 0;
+ if (va1 && !va2)
+ return m2;
+ if (va2 && !va1)
+ return m1;
+
+ throw new AmbiguousMatchException ();
+ }
+
+ int CompareCloserType (Type t1, Type t2)
+ {
+ if (t1 == t2)
+ return 0;
+ if (t1.IsGenericParameter && !t2.IsGenericParameter)
+ return 1; // t2
+ if (!t1.IsGenericParameter && t2.IsGenericParameter)
+ return -1; // t1
+ if (t1.HasElementType && t2.HasElementType)
+ return CompareCloserType (
+ t1.GetElementType (),
+ t2.GetElementType ());
+
+ if (t1.IsSubclassOf (t2))
+ return -1; // t1
+ if (t2.IsSubclassOf (t1))
+ return 1; // t2
+
+ if (t1.IsInterface && Array.IndexOf (t2.GetInterfaces (), t1) >= 0)
+ return 1; // t2
+ if (t2.IsInterface && Array.IndexOf (t1.GetInterfaces (), t2) >= 0)
+ return -1; // t1
+
+ // What kind of cases could reach here?
+ return 0;
+ }
+
public override PropertyInfo SelectProperty (BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers)
{
if (match == null || match.Length == 0)
if (idxlen >= 0 && idxlen != args.Length)
continue;
- if (haveRet && !check_type (p.PropertyType, returnType))
+ if (haveRet && p.PropertyType != returnType)
continue;
int score = Int32.MaxValue - 1;