Partager via


ILGenerator.DeclareLocal Method (Type)

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

Declares a local variable of the specified type.

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

Syntax

'Declaration
Public Overridable Function DeclareLocal ( _
    localType As Type _
) As LocalBuilder
public virtual LocalBuilder DeclareLocal(
    Type localType
)

Parameters

  • localType
    Type: System.Type
    The type of the local variable.

Return Value

Type: System.Reflection.Emit.LocalBuilder
The declared local variable.

Exceptions

Exception Condition
ArgumentNullException

localType is nulla null reference (Nothing in Visual Basic).

InvalidOperationException

The containing type has been created by the TypeBuilder.CreateType method.

Remarks

The local variable is created in the current lexical scope. For example, if code is being emitted in a for loop (For loop in Visual Basic), the scope of the variable is the loop.

A local variable that is created with this overload is not pinned. To create a pinned variable for use with unmanaged pointers, use the DeclareLocal(Type, Boolean) method overload.

Examples

The following example demonstrates the use of the DeclareLocal method to declare two local variables. This code is part of a larger example provided for the BeginExceptionBlock method.

' The exception that is thrown by DoAdd.
Dim overflowType As Type = GetType(OverflowException)


' Declare local variables to hold the result of the addition, and the
' exception that is caught by the Catch block.
Dim result As LocalBuilder = adderIL.DeclareLocal(GetType(Integer))
Dim thrownException As LocalBuilder = _
                    adderIL.DeclareLocal(overflowType)
// The exception that is thrown by DoAdd.
Type overflowType = typeof(OverflowException);


// Declare local variables to hold the result of the addition, and the
// exception that is caught by the Catch block.
LocalBuilder result = adderIL.DeclareLocal(typeof(int));
LocalBuilder thrownException = adderIL.DeclareLocal(overflowType);

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.