Leggere in inglese

Condividi tramite


LocalBuilder Classe

Definizione

Rappresenta una variabile locale all'interno di un metodo o di un costruttore.

C#
public sealed class LocalBuilder : System.Reflection.LocalVariableInfo
C#
public abstract class LocalBuilder : System.Reflection.LocalVariableInfo
C#
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public sealed class LocalBuilder : System.Runtime.InteropServices._LocalBuilder
C#
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class LocalBuilder : System.Reflection.LocalVariableInfo, System.Runtime.InteropServices._LocalBuilder
Ereditarietà
LocalBuilder
Ereditarietà
LocalBuilder
Attributi
Implementazioni

Esempio

Nell'esempio seguente viene creato un metodo di static (Shared in Visual Basic) denominato Function1 che restituisce una stringa e ha un parametro di tipo Int32. Nel corpo del metodo l'esempio di codice crea LocalBuilder oggetti che rappresentano due variabili locali e imposta le informazioni sui simboli per le variabili locali. Il metodo non esegue alcuna operazione significativa, ma il corpo del metodo dimostra l'archiviazione di un parametro in una variabile locale, l'archiviazione di una stringa letterale in una variabile locale e il caricamento di una variabile locale.

C#
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;

class LocalBuilder_Sample
{
    public static void Main()
    {
        // Create an assembly.
        AssemblyName myAssemblyName = new AssemblyName();
        myAssemblyName.Name = "SampleAssembly";

        AssemblyBuilder myAssembly =
           Thread.GetDomain().DefineDynamicAssembly(myAssemblyName,
               AssemblyBuilderAccess.RunAndSave);

        // Create a module. For a single-file assembly the module
        // name is usually the same as the assembly name.
        ModuleBuilder myModule =
            myAssembly.DefineDynamicModule(myAssemblyName.Name,
                myAssemblyName.Name + ".dll", true);

        // Define a public class 'Example'.
        TypeBuilder myTypeBuilder =
            myModule.DefineType("Example", TypeAttributes.Public);

        // Create the 'Function1' public method, which takes an integer
        // and returns a string.
        MethodBuilder myMethod = myTypeBuilder.DefineMethod("Function1",
           MethodAttributes.Public | MethodAttributes.Static,
           typeof(String), new Type[] { typeof(int) });

        // Generate IL for 'Function1'. The function body demonstrates
        // assigning an argument to a local variable, assigning a
        // constant string to a local variable, and putting the contents
        // of local variables on the stack.
        ILGenerator myMethodIL = myMethod.GetILGenerator();

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

        // Store the function argument in myInt.
        myMethodIL.Emit(OpCodes.Ldarg_0 );
        myMethodIL.Emit(OpCodes.Stloc_1 );

        // Store a literal value in myString, and return the value.
        myMethodIL.Emit(OpCodes.Ldstr, "string value"  );
        myMethodIL.Emit(OpCodes.Stloc_0 );
        myMethodIL.Emit(OpCodes.Ldloc_0 );
        myMethodIL.Emit(OpCodes.Ret );

        // Create "Example" class.
        Type myType1 = myTypeBuilder.CreateType();
        Console.WriteLine("'Example' is created.");

        myAssembly.Save(myAssemblyName.Name + ".dll");
        Console.WriteLine( "'{0}' is created.", myAssemblyName.Name + ".dll" );

        // Invoke 'Function1' method of 'Example', passing the value 42.
        Object myObject2 = myType1.InvokeMember("Function1",
            BindingFlags.InvokeMethod, null, null, new Object[] { 42 });

        Console.WriteLine("Example.Function1 returned: {0}", myObject2);
    }
}
/* This code example produces the following output:

local 'myString' type is: System.String
local 'myInt' type is: System.Int32
'Example' is created.
'SampleAssembly.dll' is created.
Example.Function1 returned: string value
 */

Commenti

È possibile definire un oggetto LocalBuilder usando il metodo DeclareLocal.

Costruttori

LocalBuilder()

Inizializza una nuova istanza della classe LocalBuilder.

Proprietà

IsPinned

Ottiene un valore che indica se l'oggetto a cui fa riferimento la variabile locale viene aggiunto in memoria.

LocalIndex

Ottiene l'indice in base zero della variabile locale all'interno del corpo del metodo.

LocalType

Ottiene il tipo della variabile locale.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
SetLocalSymInfo(String)

Imposta il nome di questa variabile locale.

SetLocalSymInfo(String, Int32, Int32)

Imposta il nome e l'ambito lessicale di questa variabile locale.

SetLocalSymInfoCore(String)

In caso di override in una classe derivata, imposta il nome di questa variabile locale.

ToString()

Restituisce una stringa leggibile dall'utente che descrive la variabile locale.

(Ereditato da LocalVariableInfo)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

_LocalBuilder.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Esegue il mapping di un set di nomi a un set corrispondente di identificatori dispatch.

_LocalBuilder.GetTypeInfo(UInt32, UInt32, IntPtr)

Recupera le informazioni sul tipo per un oggetto, che può quindi essere utilizzato per ottenere le informazioni sul tipo per un'interfaccia.

_LocalBuilder.GetTypeInfoCount(UInt32)

Recupera il numero di interfacce di informazioni sul tipo fornite da un oggetto (0 o 1).

_LocalBuilder.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Fornisce l'accesso alle proprietà e ai metodi esposti da un oggetto .

Si applica a

Prodotto Versioni
.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
.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, 2.1