ILGenerator.DeclareLocal Method (Type)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Declares a local variable of the specified type.

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

public virtual LocalBuilder DeclareLocal(
	Type localType
)

Parameters

localType
Type: System.Type

A Type object that represents the type of the local variable.

Return Value

Type: System.Reflection.Emit.LocalBuilder

The declared local variable.

Exception Condition
ArgumentNullException

localType is null.

InvalidOperationException

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

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 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 code example demonstrates the use of the DeclareLocal method. This code is part of a larger code example for the LocalBuilder class.

// Create local variables named myString and myInt.
LocalBuilder myLB1 = myMethodIL.DeclareLocal(typeof(string));
myLB1.SetLocalSymInfo("myString");
Console.WriteLine("local 'myString' type is: {0}", myLB1.LocalType);

LocalBuilder myLB2 = myMethodIL.DeclareLocal(typeof(int));
myLB2.SetLocalSymInfo("myInt", 1, 2);
Console.WriteLine("local 'myInt' type is: {0}", myLB2.LocalType);

.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.1
Return to top
Show: