ILGenerator.MarkLabel Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Marks the Microsoft intermediate language (MSIL) stream's current position with the given label.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable Sub MarkLabel ( _
    loc As Label _
)
public virtual void MarkLabel(
    Label loc
)

Parameters

Exceptions

Exception Condition
ArgumentException

loc represents an invalid index into the label array.

-or-

An index for loc has already been defined.

Remarks

A label cannot be defined more than once.

Examples

The following example demonstrates the use of MarkLabel to establish the location of a label that was defined earlier. Notice that the label is used in a call to the Emit(OpCode, Label) method overload before its location has been established.

This code is part of a larger example provided for the BeginExceptionBlock method.

adderIL.BeginFinallyBlock()

' The finally block block displays a message on the TextBlock, if the 
' OutputBlock field has been set. Just as in the catch block, the following
' code tests whether the TextBlock is present, and skips the output if it is
' not present.
adderIL.Emit(OpCodes.Ldsfld, demoOutput)
adderIL.Emit(OpCodes.Ldnull)
adderIL.Emit(OpCodes.Ceq)
adderIL.Emit(OpCodes.Brtrue_S, skipOutputInFinally)

' Append a constant string to the Text property of the TextBlock.
adderIL.Emit(OpCodes.Ldsfld, demoOutput)
adderIL.Emit(OpCodes.Ldsfld, demoOutput)
adderIL.Emit(OpCodes.Callvirt, getter)
adderIL.Emit(OpCodes.Ldstr, "Executing the finally block." & vbLf)
adderIL.Emit(OpCodes.Call, concat2)
adderIL.Emit(OpCodes.Callvirt, setter)

adderIL.MarkLabel(skipOutputInFinally)
' This is the end of the try/catch/finally block.
adderIL.EndExceptionBlock()
adderIL.BeginFinallyBlock();

// The finally block block displays a message on the TextBlock, if the 
// OutputBlock field has been set. Just as in the catch block, the following
// code tests whether the TextBlock is present, and skips the output if it is
// not present.
adderIL.Emit(OpCodes.Ldsfld, demoOutput);
adderIL.Emit(OpCodes.Ldnull);
adderIL.Emit(OpCodes.Ceq);
adderIL.Emit(OpCodes.Brtrue_S, skipOutputInFinally);

// Append a constant string to the Text property of the TextBlock.
adderIL.Emit(OpCodes.Ldsfld, demoOutput);
adderIL.Emit(OpCodes.Ldsfld, demoOutput);
adderIL.Emit(OpCodes.Callvirt, getter);
adderIL.Emit(OpCodes.Ldstr, "Executing the finally block.\n");
adderIL.Emit(OpCodes.Call, concat2);
adderIL.Emit(OpCodes.Callvirt, setter);

adderIL.MarkLabel(skipOutputInFinally);
// This is the end of the try/catch/finally block.
adderIL.EndExceptionBlock();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.