다음을 통해 공유


Expression.Label 메서드

정의

레이블을 나타내는 LabelTarget을 만듭니다.

오버로드

Label()

이름이 없는 void 형식의 레이블을 나타내는 LabelTarget을 만듭니다.

Label(LabelTarget)

기본값이 없는 레이블을 나타내는 LabelExpression을 만듭니다.

Label(String)

void 형식과 지정된 이름을 사용하여 레이블을 나타내는 LabelTarget을 만듭니다.

Label(Type)

지정된 유형을 사용하여 레이블을 나타내는 LabelTarget을 만듭니다.

Label(LabelTarget, Expression)

지정된 기본값을 사용하여 레이블을 나타내는 LabelExpression을 만듭니다.

Label(Type, String)

지정된 형식과 이름을 사용하여 레이블을 나타내는 LabelTarget을 만듭니다.

Label()

Source:
LabelTarget.cs
Source:
LabelTarget.cs
Source:
LabelTarget.cs

이름이 없는 void 형식의 레이블을 나타내는 LabelTarget을 만듭니다.

public:
 static System::Linq::Expressions::LabelTarget ^ Label();
public static System.Linq.Expressions.LabelTarget Label ();
static member Label : unit -> System.Linq.Expressions.LabelTarget
Public Shared Function Label () As LabelTarget

반환

LabelTarget입니다.

예제

다음 예제에서는 개체를 포함하는 식을 만드는 방법을 보여 줍니다 LabelTarget .

// Add the following directive to the file:
// using System.Linq.Expressions;

// A label expression of the void type that is the target for Expression.Return().
LabelTarget returnTarget = Expression.Label();

// This block contains a GotoExpression that represents a return statement with no value.
// It transfers execution to a label expression that is initialized with the same LabelTarget as the GotoExpression.
// The types of the GotoExpression, label expression, and LabelTarget must match.
BlockExpression blockExpr =
    Expression.Block(
        Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("Return")),
        Expression.Return(returnTarget),
        Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("Other Work")),
        Expression.Label(returnTarget)
    );

// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda<Action>(blockExpr).Compile()();

// This code example produces the following output:
//
// Return

// "Other Work" is not printed because
// the Return expression transfers execution from Expression.Return(returnTarget)
// to Expression.Label(returnTarget).
' Add the following directive to the file:
' Imports System.Linq.Expressions  

' A label expression of the void type that is the target for Expression.Return().
Dim returnTarget As LabelTarget = Expression.Label()

' This block contains a GotoExpression that represents a return statement with no value.
' It transfers execution to a label expression that is initialized with the same LabelTarget as the GotoExpression.
' The types of the GotoExpression, label expression, and LabelTarget must match.
Dim blockExpr As BlockExpression =
      Expression.Block(
          Expression.Call(GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}), Expression.Constant("Return")),
          Expression.Return(returnTarget),
          Expression.Call(GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}), Expression.Constant("Other Work")),
          Expression.Label(returnTarget)
      )

' The following statement first creates an expression tree,
' then compiles it, and then runs it.
Expression.Lambda(Of Action)(blockExpr).Compile()()

' This code example produces the following output:
'
' Return

' "Other Work" is not printed because 
' the Return expression transfers execution from Return(returnTarget)
' to Expression.Label(returnTarget).

적용 대상

Label(LabelTarget)

Source:
LabelExpression.cs
Source:
LabelExpression.cs
Source:
LabelExpression.cs

기본값이 없는 레이블을 나타내는 LabelExpression을 만듭니다.

public:
 static System::Linq::Expressions::LabelExpression ^ Label(System::Linq::Expressions::LabelTarget ^ target);
public static System.Linq.Expressions.LabelExpression Label (System.Linq.Expressions.LabelTarget target);
static member Label : System.Linq.Expressions.LabelTarget -> System.Linq.Expressions.LabelExpression
Public Shared Function Label (target As LabelTarget) As LabelExpression

매개 변수

target
LabelTarget

LabelTarget과 연결될 LabelExpression입니다.

반환

기본값이 없는 LabelExpression입니다.

적용 대상

Label(String)

Source:
LabelTarget.cs
Source:
LabelTarget.cs
Source:
LabelTarget.cs

void 형식과 지정된 이름을 사용하여 레이블을 나타내는 LabelTarget을 만듭니다.

public:
 static System::Linq::Expressions::LabelTarget ^ Label(System::String ^ name);
public static System.Linq.Expressions.LabelTarget Label (string name);
public static System.Linq.Expressions.LabelTarget Label (string? name);
static member Label : string -> System.Linq.Expressions.LabelTarget
Public Shared Function Label (name As String) As LabelTarget

매개 변수

name
String

레이블의 이름입니다.

반환

LabelTarget입니다.

적용 대상

Label(Type)

Source:
LabelTarget.cs
Source:
LabelTarget.cs
Source:
LabelTarget.cs

지정된 유형을 사용하여 레이블을 나타내는 LabelTarget을 만듭니다.

public:
 static System::Linq::Expressions::LabelTarget ^ Label(Type ^ type);
public static System.Linq.Expressions.LabelTarget Label (Type type);
static member Label : Type -> System.Linq.Expressions.LabelTarget
Public Shared Function Label (type As Type) As LabelTarget

매개 변수

type
Type

레이블로 이동할 때 전달되는 값의 형식입니다.

반환

LabelTarget입니다.

예제

다음 예제에서는 루프 식에서 개체를 LabelTarget 사용하는 방법을 보여 줍니다.

// Add the following directive to the file:
// using System.Linq.Expressions;

// Creating a parameter expression.
ParameterExpression value = Expression.Parameter(typeof(int), "value");

// Creating an expression to hold a local variable.
ParameterExpression result = Expression.Parameter(typeof(int), "result");

// Creating a label to jump to from a loop.
LabelTarget label = Expression.Label(typeof(int));

// Creating a method body.
BlockExpression block = Expression.Block(
    new[] { result },
    Expression.Assign(result, Expression.Constant(1)),
        Expression.Loop(
           Expression.IfThenElse(
               Expression.GreaterThan(value, Expression.Constant(1)),
               Expression.MultiplyAssign(result,
                   Expression.PostDecrementAssign(value)),
               Expression.Break(label, result)
           ),
       label
    )
);

// Compile and run an expression tree.
int factorial = Expression.Lambda<Func<int, int>>(block, value).Compile()(5);

Console.WriteLine(factorial);

// This code example produces the following output:
//
// 120
' Add the following directive to the file:
' Imports System.Linq.Expressions  

' Creating a parameter expression.
Dim value As ParameterExpression =
    Expression.Parameter(GetType(Integer), "value")

' Creating an expression to hold a local variable. 
Dim result As ParameterExpression =
    Expression.Parameter(GetType(Integer), "result")

' Creating a label to jump to from a loop.
Dim label As LabelTarget = Expression.Label(GetType(Integer))

' Creating a method body.
Dim block As BlockExpression = Expression.Block(
    New ParameterExpression() {result},
    Expression.Assign(result, Expression.Constant(1)),
    Expression.Loop(
        Expression.IfThenElse(
            Expression.GreaterThan(value, Expression.Constant(1)),
            Expression.MultiplyAssign(result,
                Expression.PostDecrementAssign(value)),
            Expression.Break(label, result)
        ),
        label
    )
)

' Compile an expression tree and return a delegate.
Dim factorial As Integer =
    Expression.Lambda(Of Func(Of Integer, Integer))(block, value).Compile()(5)

Console.WriteLine(factorial)

' This code example produces the following output:
'
' 120

적용 대상

Label(LabelTarget, Expression)

Source:
LabelExpression.cs
Source:
LabelExpression.cs
Source:
LabelExpression.cs

지정된 기본값을 사용하여 레이블을 나타내는 LabelExpression을 만듭니다.

public:
 static System::Linq::Expressions::LabelExpression ^ Label(System::Linq::Expressions::LabelTarget ^ target, System::Linq::Expressions::Expression ^ defaultValue);
public static System.Linq.Expressions.LabelExpression Label (System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression defaultValue);
public static System.Linq.Expressions.LabelExpression Label (System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression? defaultValue);
static member Label : System.Linq.Expressions.LabelTarget * System.Linq.Expressions.Expression -> System.Linq.Expressions.LabelExpression
Public Shared Function Label (target As LabelTarget, defaultValue As Expression) As LabelExpression

매개 변수

target
LabelTarget

LabelTarget과 연결될 LabelExpression입니다.

defaultValue
Expression

일반 제어 흐름을 통해 레이블에 접근하는 경우 이 LabelExpression의 값입니다.

반환

지정된 기본값이 있는 LabelExpression입니다.

적용 대상

Label(Type, String)

Source:
LabelTarget.cs
Source:
LabelTarget.cs
Source:
LabelTarget.cs

지정된 형식과 이름을 사용하여 레이블을 나타내는 LabelTarget을 만듭니다.

public:
 static System::Linq::Expressions::LabelTarget ^ Label(Type ^ type, System::String ^ name);
public static System.Linq.Expressions.LabelTarget Label (Type type, string name);
public static System.Linq.Expressions.LabelTarget Label (Type type, string? name);
static member Label : Type * string -> System.Linq.Expressions.LabelTarget
Public Shared Function Label (type As Type, name As String) As LabelTarget

매개 변수

type
Type

레이블로 이동할 때 전달되는 값의 형식입니다.

name
String

레이블의 이름입니다.

반환

LabelTarget입니다.

적용 대상