public static ParameterAssignment Bind(MethodBase method, string parameter, Expression expression)
        {
            // NB: This overload is needed for the compiler to emit factory calls;
            //     we can't emit a `ldtoken` instruction to obtain a ParameterInfo.

            ContractUtils.RequiresNotNull(method, nameof(method));
            ContractUtils.RequiresNotNull(parameter, nameof(parameter));

            var parameterInfo = default(ParameterInfo);

            foreach (var candidate in method.GetParametersCached())
            {
                if (candidate.Name == parameter)
                {
                    parameterInfo = candidate;
                    break;
                }
            }

            if (parameterInfo == null)
            {
                throw Error.ParameterNotDefinedForMethod(parameter, method.Name);
            }

            return Bind(parameterInfo, expression);
        }
Esempio n. 2
0
 private static bool IsCompatible(MethodBase m, Expression[] args)
 {
     ParameterInfo[] parms = m.GetParametersCached();
     if (parms.Length != args.Length)
         return false;
     for (int i = 0; i < args.Length; i++)
     {
         Expression arg = args[i];
         ContractUtils.RequiresNotNull(arg, "argument");
         Type argType = arg.Type;
         Type pType = parms[i].ParameterType;
         if (pType.IsByRef)
         {
             pType = pType.GetElementType();
         }
         if (!TypeUtils.AreReferenceAssignable(pType, argType) &&
             !(TypeUtils.IsSameOrSubclass(typeof(LambdaExpression), pType) && pType.IsAssignableFrom(arg.GetType())))
         {
             return false;
         }
     }
     return true;
 }
        /// <summary>
        /// Emits arguments to a call, and returns an array of write-backs that
        /// should happen after the call. For emitting dynamic expressions, we
        /// need to skip the first parameter of the method (the call site).
        /// </summary>
        private List<WriteBack> EmitArguments(MethodBase method, IArgumentProvider args, int skipParameters)
        {
            ParameterInfo[] pis = method.GetParametersCached();
            Debug.Assert(args.ArgumentCount + skipParameters == pis.Length);

            var writeBacks = new List<WriteBack>();
            for (int i = skipParameters, n = pis.Length; i < n; i++)
            {
                ParameterInfo parameter = pis[i];
                Expression argument = args.GetArgument(i - skipParameters);
                Type type = parameter.ParameterType;

                if (type.IsByRef)
                {
                    type = type.GetElementType();

                    WriteBack wb = EmitAddressWriteBack(argument, type);
                    if (wb != null)
                    {
                        writeBacks.Add(wb);
                    }
                }
                else
                {
                    EmitExpression(argument);
                }
            }
            return writeBacks;
        }
        private static ParameterInfo[] GetParametersForValidation(MethodBase method, ExpressionType nodeKind) {
            ParameterInfo[] pis = method.GetParametersCached();

            if (nodeKind == ExpressionType.Dynamic) {
                pis = pis.RemoveFirst(); // ignore CallSite argument
            }
            return pis;
        }
            /// <summary>
            /// Marks child expressions representing arguments bound to parameters of
            /// the specified <paramref name="method"/> as ByRef values if needed.
            /// </summary>
            /// <param name="method">
            /// The method containing the parameters bound to the arguments held by
            /// child expressions tracked by this rewriter.
            /// </param>
            /// <param name="startIndex">
            /// The index of the child expression representing the first argument. This
            /// value is typically 0 for static methods and 1 for instance methods.
            /// </param>
            internal void MarkRefArgs(MethodBase method, int startIndex)
            {
                var parameters = method.GetParametersCached();

                for (int i = 0, n = parameters.Length; i < n;  i++)
                {
                    if (parameters[i].ParameterType.IsByRef)
                    {
                        MarkRef(startIndex + i);
                    }
                }
            }
Esempio n. 6
0
 public static ReadOnlyCollection<ParameterAssignment> GetParameterBindings(MethodBase method, IEnumerable<Expression> expressions)
 {
     return GetParameterBindings(method.GetParametersCached(), expressions);
 }
Esempio n. 7
0
 public static void ValidateParameterBindings(MethodBase method, ReadOnlyCollection<ParameterAssignment> argList, bool extensionMethod = false)
 {
     ValidateParameterBindings(method, method.GetParametersCached(), argList, extensionMethod);
 }