ILGenerator.DeclareLocal Method (Type)
February 14, 2014
Declares a local variable of the specified type.
Assembly: mscorlib (in mscorlib.dll)
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.
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. 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);