private static Object[] LickArgumentsIntoShape(ParameterInfo[] pars, Object[] arguments, Binder binder, CultureInfo culture){
if (arguments == null)
return null;
int formals = pars.Length;
if (formals == 0)
return null;
Object[] newArgs = arguments;
int actuals = arguments.Length;
if (actuals != formals)
newArgs = new Object[formals];
int m = formals-1; //Position of last formal param
int k = actuals < m ? actuals : m; //Number of formal params that have corresponding actual arguments, bar the last one, which may be a varArg array.
//Copy all actual args that match formal param, bar the last one. Change DBNull to null. Coerce actual value to formal param type.
for (int i = 0; i < k; i++){
Object arg = arguments[i];
if (arg is DBNull)
newArgs[i] = null; //Outside of the world of OLE Variants, undefined == null == the null pointer.
else
newArgs[i] = binder.ChangeType(arguments[i], pars[i].ParameterType, culture);
}
//Supply default values for all formal params, bar the last one, that do not have corresponding actual arguments
for (int i = k; i < m; i++){
Object dv = TypeReferences.GetDefaultParameterValue(pars[i]);
if (dv == System.Convert.DBNull) //No default value was specified
dv = binder.ChangeType(null, pars[i].ParameterType, culture); //Substitute undefined
newArgs[i] = dv;
}
//If the last formal param is a vararg param, treat it specially.
if (CustomAttribute.IsDefined(pars[m], typeof(ParamArrayAttribute), false)){
int numVarArgs = actuals - m;
if (numVarArgs < 0) numVarArgs = 0;
Type t = pars[m].ParameterType.GetElementType();
Array pa = Array.CreateInstance(t, numVarArgs);
for (int j = 0; j < numVarArgs; j++)
pa.SetValue(binder.ChangeType(arguments[j+m], t, culture), j);
newArgs[m] = pa;
}else if (actuals < formals){
Object dv = TypeReferences.GetDefaultParameterValue(pars[m]);
if (dv == System.Convert.DBNull) //No default value was specified
dv = binder.ChangeType(null, pars[m].ParameterType, culture); //Substitute undefined
newArgs[m] = dv;
}else
newArgs[m] = binder.ChangeType(arguments[m], pars[m].ParameterType, culture);
return newArgs;
}