Прочетете на английски Редактиране

Споделяне чрез


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.

C#
public abstract System.Reflection.Emit.LocalBuilder DeclareLocal(Type localType, bool pinned);
C#
public virtual System.Reflection.Emit.LocalBuilder DeclareLocal(Type localType, bool pinned);

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

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1

DeclareLocal(Type)

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

Declares a local variable of the specified type.

C#
public virtual System.Reflection.Emit.LocalBuilder DeclareLocal(Type localType);
C#
public System.Reflection.Emit.LocalBuilder DeclareLocal(Type localType);

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.

C#
// 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);

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

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1