/// <param name="type">The vararg class type (aka component type
            /// of the expected array arg)
            /// </param>
            /// <param name="index">The index of the vararg in the method declaration
            /// (This will always be one less than the number of
            /// expected arguments.)
            /// </param>
            /// <param name="parameters">The parameters parameters being passed to this method
            /// </param>
            /// <returns>s The parameters parameters adjusted for the varargs in order
            /// to fit the method declaration.
            /// </returns>
            private static object[] handleVarArg(System.Type type, int index, object[] actual)
            {
                // if no values are being passed into the vararg
                if (actual.Length == index)
                {
                    // copy existing args to new array
                    object[] newActual = new object[actual.Length + 1];
                    Array.Copy(actual, 0, newActual, 0, actual.Length);
                    // create an empty array of the expected type
                    newActual[index] = System.Array.CreateInstance(type, 0);
                    actual           = newActual;
                }
                // if one value is being passed into the vararg
                else if (actual.Length == index + 1)
                {
                    // make sure the last arg is an array of the expected type
                    if (IntrospectionUtils.IsMethodInvocationConvertible(type, actual[index].GetType(), false))
                    {
                        // create a 1-length array to hold and replace the last param
                        object lastActual = System.Array.CreateInstance(type, 1);
                        ((System.Array)lastActual).SetValue(actual[index], 0);
                        actual[index] = lastActual;
                    }
                }
                // if multiple values are being passed into the vararg
                else if (actual.Length > index + 1)
                {
                    // Put the last and extra parameters in an array of the expected type
                    int    size       = actual.Length - index;
                    object lastActual = System.Array.CreateInstance(type, size);
                    for (int i = 0; i < size; i++)
                    {
                        ((System.Array)lastActual).SetValue(actual[index + i], i);
                    }

                    // Put all into a new parameters array of the appropriate size
                    object[] newActual = new object[index + 1];
                    for (int i = 0; i < index; i++)
                    {
                        newActual[i] = actual[i];
                    }
                    newActual[index] = lastActual;

                    // replace the old parameters array
                    actual = newActual;
                }
                return(actual);
            }
Example #2
0
 private static bool isConvertible(System.Type formal, System.Type actual, bool possibleVarArg)
 {
     return(IntrospectionUtils.IsMethodInvocationConvertible(formal, actual, possibleVarArg));
 }