Επεξεργασία

Κοινή χρήση μέσω


ILGenerator.DeclareLocal Method

Definition

Declares a local variable.

Overloads

DeclareLocal(Type, Boolean)

Declares a local variable of the specified type, optionally pinning the object referred to by the variable.

DeclareLocal(Type)

Declares a local variable of the specified type.

DeclareLocal(Type, Boolean)

Source:
ILGenerator.cs
Source:
ILGenerator.cs
Source:
ILGenerator.cs

Declares a local variable of the specified type, optionally pinning the object referred to by the variable.

public:
 virtual System::Reflection::Emit::LocalBuilder ^ DeclareLocal(Type ^ localType, bool pinned);
public:
 abstract System::Reflection::Emit::LocalBuilder ^ DeclareLocal(Type ^ localType, bool pinned);
public virtual System.Reflection.Emit.LocalBuilder DeclareLocal (Type localType, bool pinned);
public abstract System.Reflection.Emit.LocalBuilder DeclareLocal (Type localType, bool pinned);
abstract member DeclareLocal : Type * bool -> System.Reflection.Emit.LocalBuilder
override this.DeclareLocal : Type * bool -> System.Reflection.Emit.LocalBuilder
abstract member DeclareLocal : Type * bool -> System.Reflection.Emit.LocalBuilder
Public Overridable Function DeclareLocal (localType As Type, pinned As Boolean) As LocalBuilder
Public MustOverride Function DeclareLocal (localType As Type, pinned As Boolean) As LocalBuilder

Parameters

localType
Type

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

pinned
Boolean

true to pin the object in memory; otherwise, false.

Returns

A LocalBuilder object that represents the local variable.

Exceptions

localType is null.

The containing type has been created by the CreateType() method.

-or-

The method body of the enclosing method has been created by the CreateMethodBody(Byte[], Int32) method.

The method with which this ILGenerator is associated is not represented by a MethodBuilder.

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.

In unsafe code, an object must be pinned before it can be referred to by an unmanaged pointer. While the referenced object is pinned, it cannot be moved by garbage collection.

Applies to

DeclareLocal(Type)

Source:
ILGenerator.cs
Source:
ILGenerator.cs
Source:
ILGenerator.cs

Declares a local variable of the specified type.

public:
 virtual System::Reflection::Emit::LocalBuilder ^ DeclareLocal(Type ^ localType);
public:
 System::Reflection::Emit::LocalBuilder ^ DeclareLocal(Type ^ localType);
public virtual System.Reflection.Emit.LocalBuilder DeclareLocal (Type localType);
public System.Reflection.Emit.LocalBuilder DeclareLocal (Type localType);
abstract member DeclareLocal : Type -> System.Reflection.Emit.LocalBuilder
override this.DeclareLocal : Type -> System.Reflection.Emit.LocalBuilder
member this.DeclareLocal : Type -> System.Reflection.Emit.LocalBuilder
Public Overridable Function DeclareLocal (localType As Type) As LocalBuilder
Public Function DeclareLocal (localType As Type) As LocalBuilder

Parameters

localType
Type

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

Returns

The declared local variable.

Exceptions

localType is null.

The containing type has been created by the CreateType() method.

Examples

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( String::typeid );
myLB1->SetLocalSymInfo( "myString" );
Console::WriteLine( "local 'myString' type is: {0}", myLB1->LocalType );

LocalBuilder^ myLB2 = myMethodIL->DeclareLocal( int::typeid );
myLB2->SetLocalSymInfo( "myInt", 1, 2 );
Console::WriteLine( "local 'myInt' type is: {0}", myLB2->LocalType );
// 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);
' Create local variables named myString and myInt.
Dim myLB1 As LocalBuilder = myMethodIL.DeclareLocal(GetType(String))
myLB1.SetLocalSymInfo("myString")
Console.WriteLine("local 'myString' type is: {0}", myLB1.LocalType)

Dim myLB2 As LocalBuilder = myMethodIL.DeclareLocal(GetType(Integer))
myLB2.SetLocalSymInfo("myInt", 1, 2)
Console.WriteLine("local 'myInt' type is: {0}", myLB2.LocalType)

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 created with this overload is not pinned. To create a pinned variable for use with unmanaged pointers, use the DeclareLocal(Type, Boolean) method overload.

Applies to