LambdaExpression Classe
Definição
Descreve uma expressão lambda.Describes a lambda expression. Isso captura um bloco de código semelhante a um corpo de método do .NET.This captures a block of code that is similar to a .NET method body.
public ref class LambdaExpression abstract : System::Linq::Expressions::Expression
public ref class LambdaExpression : System::Linq::Expressions::Expression
public abstract class LambdaExpression : System.Linq.Expressions.Expression
public class LambdaExpression : System.Linq.Expressions.Expression
type LambdaExpression = class
inherit Expression
Public MustInherit Class LambdaExpression
Inherits Expression
Public Class LambdaExpression
Inherits Expression
- Herança
- Derivado
Exemplos
O exemplo a seguir demonstra como criar uma expressão que representa uma expressão lambda que adiciona 1 ao argumento passado usando o Lambda método.The following example demonstrates how to create an expression that represents a lambda expression that adds 1 to the passed argument by using the Lambda method.
// Add the following directive to your file:
// using System.Linq.Expressions;
// A parameter for the lambda expression.
ParameterExpression paramExpr = Expression.Parameter(typeof(int), "arg");
// This expression represents a lambda expression
// that adds 1 to the parameter value.
LambdaExpression lambdaExpr = Expression.Lambda(
Expression.Add(
paramExpr,
Expression.Constant(1)
),
new List<ParameterExpression>() { paramExpr }
);
// Print out the expression.
Console.WriteLine(lambdaExpr);
// Compile and run the lamda expression.
// The value of the parameter is 1.
Console.WriteLine(lambdaExpr.Compile().DynamicInvoke(1));
// This code example produces the following output:
//
// arg => (arg +1)
// 2
' Add the following directive to your file:
' Imports System.Linq.Expressions
' A parameter for the lambda expression.
Dim paramExpr As ParameterExpression = Expression.Parameter(GetType(Integer), "arg")
' This expression represents a lambda expression
' that adds 1 to the parameter value.
Dim lambdaExpr As LambdaExpression = Expression.Lambda(
Expression.Add(
paramExpr,
Expression.Constant(1)
),
New List(Of ParameterExpression)() From {paramExpr}
)
' Print out the expression.
Console.WriteLine(lambdaExpr)
' Compile and run the lamda expression.
' The value of the parameter is 1.
Console.WriteLine(lambdaExpr.Compile().DynamicInvoke(1))
' This code example produces the following output:
'
' arg => (arg +1)
' 2
Comentários
O LambdaExpression tipo representa uma expressão lambda na forma de uma árvore de expressão.The LambdaExpression type represents a lambda expression in the form of an expression tree. O Expression<TDelegate> tipo, que deriva LambdaExpression e captura o tipo da expressão lambda mais explicitamente, também pode ser usado para representar uma expressão lambda.The Expression<TDelegate> type, which derives from LambdaExpression and captures the type of the lambda expression more explicitly, can also be used to represent a lambda expression. Em tempo de execução, um nó de árvore de expressão que representa uma expressão lambda é sempre do tipo Expression<TDelegate> .At runtime, an expression tree node that represents a lambda expression is always of type Expression<TDelegate>.
O valor da NodeType propriedade de a LambdaExpression é Lambda .The value of the NodeType property of a LambdaExpression is Lambda.
Use os Lambda métodos de fábrica para criar um LambdaExpression objeto.Use the Lambda factory methods to create a LambdaExpression object.
Propriedades
| Body |
Obtém o corpo da expressão lambda.Gets the body of the lambda expression. |
| CanReduce |
Indica que o nó pode ser reduzido a um nó mais simples.Indicates that the node can be reduced to a simpler node. Se isso retornar verdadeiro, Reduce() poderá ser chamado para produzir a forma reduzida.If this returns true, Reduce() can be called to produce the reduced form. (Herdado de Expression) |
| Name |
Obtém o nome da expressão lambda.Gets the name of the lambda expression. |
| NodeType |
Retorna o tipo de nó deste Expression.Returns the node type of this Expression. |
| NodeType |
Obtém o tipo de nó deste Expression.Gets the node type of this Expression. (Herdado de Expression) |
| Parameters |
Obtém os parâmetros da expressão lambda.Gets the parameters of the lambda expression. |
| ReturnType |
Obtém o tipo de retorno da expressão lambda.Gets the return type of the lambda expression. |
| TailCall |
Obtém o valor que indica se a expressão lambda será compilada com a otimização de chamada tail.Gets the value that indicates if the lambda expression will be compiled with the tail call optimization. |
| Type |
Obtém o tipo estático da expressão que esse Expression representa.Gets the static type of the expression that this Expression represents. |
| Type |
Obtém o tipo estático da expressão que esse Expression representa.Gets the static type of the expression that this Expression represents. (Herdado de Expression) |
Métodos
| Accept(ExpressionVisitor) |
Expede o método específico de visita para esse tipo de nó.Dispatches to the specific visit method for this node type. Por exemplo, MethodCallExpression chama o VisitMethodCall(MethodCallExpression).For example, MethodCallExpression calls the VisitMethodCall(MethodCallExpression). (Herdado de Expression) |
| Compile() |
Produz um delegado que representa a expressão lambda.Produces a delegate that represents the lambda expression. |
| Compile(Boolean) |
Produz um delegado interpretado ou compilado que representa a expressão lambda.Produces an interpreted or compiled delegate that represents the lambda expression. |
| Compile(DebugInfoGenerator) |
Produz um delegado que representa a expressão lambda.Produces a delegate that represents the lambda expression. |
| CompileToMethod(MethodBuilder) |
Compila o lambda em uma definição de método.Compiles the lambda into a method definition. |
| CompileToMethod(MethodBuilder, DebugInfoGenerator) |
Compila o lambda em uma definição de método e as informações de depuração personalizada.Compiles the lambda into a method definition and custom debug information. |
| Equals(Object) |
Determina se o objeto especificado é igual ao objeto atual.Determines whether the specified object is equal to the current object. (Herdado de Object) |
| GetHashCode() |
Serve como a função de hash padrão.Serves as the default hash function. (Herdado de Object) |
| GetType() |
Obtém o Type da instância atual.Gets the Type of the current instance. (Herdado de Object) |
| MemberwiseClone() |
Cria uma cópia superficial do Object atual.Creates a shallow copy of the current Object. (Herdado de Object) |
| Reduce() |
Reduz esse nó a uma expressão mais simples.Reduces this node to a simpler expression. Se CanReduce retornar true, ele deverá retornar uma expressão válida.If CanReduce returns true, this should return a valid expression. Esse método pode retornar outro nó que também precisa ser reduzido.This method can return another node which itself must be reduced. (Herdado de Expression) |
| ReduceAndCheck() |
Reduz esse nó a uma expressão mais simples.Reduces this node to a simpler expression. Se CanReduce retornar true, ele deverá retornar uma expressão válida.If CanReduce returns true, this should return a valid expression. Esse método pode retornar outro nó que também precisa ser reduzido.This method can return another node which itself must be reduced. (Herdado de Expression) |
| ReduceExtensions() |
Reduz a expressão a um tipo de nó conhecido (que não é um nó de extensão) ou apenas retorna a expressão se ele já é um tipo conhecido.Reduces the expression to a known node type (that is not an Extension node) or just returns the expression if it is already a known type. (Herdado de Expression) |
| ToString() |
Retorna uma representação textual de Expression.Returns a textual representation of the Expression. (Herdado de Expression) |
| VisitChildren(ExpressionVisitor) |
Reduz o nó e, em seguida, chama o delegado visitante na expressão reduzida.Reduces the node and then calls the visitor delegate on the reduced expression. O método gera uma exceção se o nó não é reduzível.The method throws an exception if the node is not reducible. (Herdado de Expression) |