public ref class Expression abstract
public abstract class Expression
type Expression = class
Public MustInherit Class Expression
Expression
下面的代码示例演示如何创建块表达式。 块表达式由两个
MethodCallExpression
对象和一个
ConstantExpression
对象组成。
// Add the following directive to your file:
// using System.Linq.Expressions;
// The block expression allows for executing several expressions sequentually.
// When the block expression is executed,
// it returns the value of the last expression in the sequence.
BlockExpression blockExpr = Expression.Block(
Expression.Call(
null,
typeof(Console).GetMethod("Write", new Type[] { typeof(String) }),
Expression.Constant("Hello ")
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("World!")
Expression.Constant(42)
Console.WriteLine("The result of executing the expression tree:");
// The following statement first creates an expression tree,
// then compiles it, and then executes it.
var result = Expression.Lambda<Func<int>>(blockExpr).Compile()();
// Print out the expressions from the block expression.
Console.WriteLine("The expressions from the block expression:");
foreach (var expr in blockExpr.Expressions)
Console.WriteLine(expr.ToString());
// Print out the result of the tree execution.
Console.WriteLine("The return value of the block expression:");
Console.WriteLine(result);
// This code example produces the following output:
// The result of executing the expression tree:
// Hello World!
// The expressions from the block expression:
// Write("Hello ")
// WriteLine("World!")
// 42
// The return value of the block expression:
// 42
' Add the following directive to your file:
' Imports System.Linq.Expressions
' The block expression enables you to execute several expressions sequentually.
' When the block expression is executed,
' it returns the value of the last expression in the sequence.
Dim blockExpr As BlockExpression = Expression.Block(
Expression.Call(
Nothing,
GetType(Console).GetMethod("Write", New Type() {GetType(String)}),
Expression.Constant("Hello ")
Expression.Call(
Nothing,
GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}),
Expression.Constant("World!")
Expression.Constant(42)
Console.WriteLine("The result of executing the expression tree:")
' The following statement first creates an expression tree,
' then compiles it, and then executes it.
Dim result = Expression.Lambda(Of Func(Of Integer))(blockExpr).Compile()()
' Print the expressions from the block expression.
Console.WriteLine("The expressions from the block expression:")
For Each expr In blockExpr.Expressions
Console.WriteLine(expr.ToString())
' Print the result of the tree execution.
Console.WriteLine("The return value of the block expression:")
Console.WriteLine(result)
' This code example produces the following output:
' The result of executing the expression tree:
' Hello World!
' The expressions from the block expression:
' Write("Hello ")
' WriteLine("World!")
' The return value of the block expression: