Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/runtime/Binding/Binders.cs (C#)

Code before:
                }

                // The extension method 'CustomAttributeExtensions.GetCustomAttributes(ParameterInfo, Type, Boolean)' has inconsistent
                // behavior on its return value in both FullCLR and CoreCLR. According to MSDN, if the attribute cannot be found, it
                // should return an empty collection. However, it returns null in some rare cases [when the parameter isn't backed by
                // actual metadata].
                // This inconsistent behavior affects OneCore powershell because we are using the extension method here when compiling
                // against CoreCLR. So we need to add a null check until this is fixed in CLR.
                var paramArrayAttrs = parameters[i].GetCustomAttributes(typeof(ParamArrayAttribute), false);
                if (paramArrayAttrs != null && paramArrayAttrs.Length > 0)
                {
                    Diagnostics.Assert(i == parameters.Length - 1, "vararg parameter is not the last");
                    var paramElementType = parameterType.GetElementType();

                    if (expandParameters)
                    {
                        IEnumerable<Expression> elements = args
                            .Skip(i)
                            .Select(a =>
                                a.CastOrConvertMethodArgument(
                                    paramElementType,
                                    paramName,
                                    mi.Name,
                                    allowCastingToByRefLikeType: false,
                                    temps,
                                    initTemps));

                        argExprs[i] = Expression.NewArrayInit(paramElementType, elements);
                        // User specified the element arguments, so we log them instead of the compiler-created array.
                        argsToLog.AddRange(elements);
                    }
                    else
                    {
                        var arg = args[i].CastOrConvertMethodArgument(
                            parameterType,
                            paramName,
                            mi.Name,
                            allowCastingToByRefLikeType: false,
                            temps,
                            initTemps);

                        argExprs[i] = arg;
                        argsToLog.Add(arg);
                    }
                }
                else if (i >= args.Length)
                {
                    // We don't log the default value for an optional parameter, as it's not specified by the user.
                    Diagnostics.Assert(
                        parameters[i].IsOptional,
                        "if there are too few arguments, FindBestMethod should only succeed if parameters are optional");

Review comment (suggestion):
So there won't be doubles of the actual code in the compiled result, but `CastOrConvertMethodArgument` will still run twice. It's probably worth doing `.ToArray()` here to avoid that

Code after:
                }

                // The extension method 'CustomAttributeExtensions.GetCustomAttributes(ParameterInfo, Type, Boolean)' has inconsistent
                // behavior on its return value in both FullCLR and CoreCLR. According to MSDN, if the attribute cannot be found, it
                // should return an empty collection. However, it returns null in some rare cases [when the parameter isn't backed by
                // actual metadata].
                // This inconsistent behavior affects OneCore powershell because we are using the extension method here when compiling
                // against CoreCLR. So we need to add a null check until this is fixed in CLR.
                var paramArrayAttrs = parameters[i].GetCustomAttributes(typeof(ParamArrayAttribute), false);
                if (paramArrayAttrs != null && paramArrayAttrs.Length > 0)
                {
                    Diagnostics.Assert(i == parameters.Length - 1, "vararg parameter is not the last");
                    var paramElementType = parameterType.GetElementType();

                    if (expandParameters)
                    {
                        IEnumerable<Expression> elements = args
                            .Skip(i)
                            .Select(a =>
                                a.CastOrConvertMethodArgument(
                                    paramElementType,
                                    paramName,
                                    mi.Name,
                                    allowCastingToByRefLikeType: false,
                                    temps,
                                    initTemps))
                            .ToList();

                        argExprs[i] = Expression.NewArrayInit(paramElementType, elements);
                        // User specified the element arguments, so we log them instead of the compiler-created array.
                        argsToLog.AddRange(elements);
                    }
                    else
                    {
                        var arg = args[i].CastOrConvertMethodArgument(
                            parameterType,
                            paramName,
                            mi.Name,
                            allowCastingToByRefLikeType: false,
                            temps,
                            initTemps);

                        argExprs[i] = arg;
                        argsToLog.Add(arg);
                    }
                }
                else if (i >= args.Length)
                {
                    // We don't log the default value for an optional parameter, as it's not specified by the user.
                    Diagnostics.Assert(
                        parameters[i].IsOptional,