Home > database >  why InvocationExpression's Arguments is Expression rather than ParameterExpression?
why InvocationExpression's Arguments is Expression rather than ParameterExpression?

Time:06-15

if you look at the source code of InvocationExpression

public sealed class InvocationExpression : Expression, IArgumentProvider
{
   public Expression Expression { get; }
   public ReadOnlyCollection<Expression> Arguments { get; }
}

why it is not public ReadOnlyCollection<ParameterExpression> Arguments { get; }, just like what LambdaExpression is, isn't that the Arguments in InvocationExpression the same thing as Parameters in LambdaExpression?

public abstract class LambdaExpression : Expression
{
   public Expression Body { get; }
   public ReadOnlyCollection<ParameterExpression> Parameters { get; }
   // ...
}

CodePudding user response:

Parameters and arguments aren't the same thing. A parameter is part of the declaration of a method, constructor, lambda expression etc. An argument is an expression evaluated to obtain the initial value of a parameter - and it can generally be any expression that's convertible to the appropriate type. If the Arguments property were of type ReadOnlyCollection<ParameterExpression>, you'd only be able to use parameters as invocation arguments... so you would be able to create an expression tree like this:

Expression<Action<string>> x = p => Console.WriteLine(p);

(because the expression p is a parameter expression)... but you wouldn't be able to write this:

Expression<Action<string>> x = p => Console.WriteLine("Hello "   p);

... because "Hello " p (the argument to Console.WriteLine) isn't a parameter; it's a binary addition operation.

  •  Tags:  
  • c#
  • Related