DynamicMethod.GetILGenerator Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Gibt einen MSIL-Generator zurück, mit dem ein Textkörper für die dynamische Methode ausgegeben werden kann.
Überlädt
| Name | Beschreibung |
|---|---|
| GetILGenerator(Int32) |
Gibt einen Microsoft MSIL-Generator (Intermediate Language) für die Methode mit der angegebenen MSIL-Datenstromgröße zurück. |
| GetILGenerator() |
Gibt einen Microsoft MSIL-Generator (Intermediate Language) für die Methode mit einer Standardmäßigen MSIL-Datenstromgröße von 64 Bytes zurück. |
GetILGenerator(Int32)
- Quelle:
- DynamicMethod.CoreCLR.cs
- Quelle:
- DynamicMethod.CoreCLR.cs
- Quelle:
- DynamicMethod.cs
- Quelle:
- DynamicMethod.CoreCLR.cs
- Quelle:
- DynamicMethod.CoreCLR.cs
Gibt einen Microsoft MSIL-Generator (Intermediate Language) für die Methode mit der angegebenen MSIL-Datenstromgröße zurück.
public:
System::Reflection::Emit::ILGenerator ^ GetILGenerator(int streamSize);
public System.Reflection.Emit.ILGenerator GetILGenerator(int streamSize);
member this.GetILGenerator : int -> System.Reflection.Emit.ILGenerator
Public Function GetILGenerator (streamSize As Integer) As ILGenerator
Parameter
- streamSize
- Int32
Die Größe des MSIL-Datenstroms in Byte.
Gibt zurück
Ein ILGenerator Objekt für die Methode mit der angegebenen MSIL-Datenstromgröße.
Beispiele
Im folgenden Codebeispiel wird diese Methodenüberladung veranschaulicht. Dieses Codebeispiel ist Teil eines größeren Beispiels, das für die DynamicMethod Klasse bereitgestellt wird.
// Create an array that specifies the parameter types of the
// overload of Console.WriteLine to be used in Hello.
Type[] writeStringArgs = {typeof(string)};
// Get the overload of Console.WriteLine that has one
// String parameter.
MethodInfo writeString = typeof(Console).GetMethod("WriteLine",
writeStringArgs);
// Get an ILGenerator and emit a body for the dynamic method,
// using a stream size larger than the IL that will be
// emitted.
ILGenerator il = hello.GetILGenerator(256);
// Load the first argument, which is a string, onto the stack.
il.Emit(OpCodes.Ldarg_0);
// Call the overload of Console.WriteLine that prints a string.
il.EmitCall(OpCodes.Call, writeString, null);
// The Hello method returns the value of the second argument;
// to do this, load the onto the stack and return.
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ret);
' Create an array that specifies the parameter types of the
' overload of Console.WriteLine to be used in Hello.
Dim writeStringArgs() As Type = {GetType(String)}
' Get the overload of Console.WriteLine that has one
' String parameter.
Dim writeString As MethodInfo = GetType(Console). _
GetMethod("WriteLine", writeStringArgs)
' Get an ILGenerator and emit a body for the dynamic method,
' using a stream size larger than the IL that will be
' emitted.
Dim il As ILGenerator = hello.GetILGenerator(256)
' Load the first argument, which is a string, onto the stack.
il.Emit(OpCodes.Ldarg_0)
' Call the overload of Console.WriteLine that prints a string.
il.EmitCall(OpCodes.Call, writeString, Nothing)
' The Hello method returns the value of the second argument;
' to do this, load the onto the stack and return.
il.Emit(OpCodes.Ldarg_1)
il.Emit(OpCodes.Ret)
Hinweise
Nachdem eine dynamische Methode abgeschlossen wurde, wird durch das Aufrufen der CreateDelegate-Methode oder der Invoke-Methode jeder weitere Versuch, MSIL hinzuzufügen, ignoriert. Es wird keine Ausnahme ausgelöst.
Note
Es gibt Einschränkungen für unverifizierbaren Code in dynamischen Methoden, auch in einigen voll vertrauenswürdigen Szenarien. Weitere Informationen finden Sie im Abschnitt "Verifizierung" in den Hinweisen für DynamicMethod.
Weitere Informationen
Gilt für:
GetILGenerator()
- Quelle:
- DynamicMethod.cs
- Quelle:
- DynamicMethod.cs
- Quelle:
- DynamicMethod.cs
- Quelle:
- DynamicMethod.cs
- Quelle:
- DynamicMethod.cs
Gibt einen Microsoft MSIL-Generator (Intermediate Language) für die Methode mit einer Standardmäßigen MSIL-Datenstromgröße von 64 Bytes zurück.
public:
System::Reflection::Emit::ILGenerator ^ GetILGenerator();
public System.Reflection.Emit.ILGenerator GetILGenerator();
member this.GetILGenerator : unit -> System.Reflection.Emit.ILGenerator
Public Function GetILGenerator () As ILGenerator
Gibt zurück
Ein ILGenerator Objekt für die Methode.
Beispiele
Im folgenden Codebeispiel wird eine dynamische Methode erstellt, die zwei Parameter verwendet. Im Beispiel wird ein einfacher Funktionstext ausgegeben, der den ersten Parameter in der Konsole druckt, und im Beispiel wird der zweite Parameter als Rückgabewert der Methode verwendet. Im Beispiel wird die Methode abgeschlossen, indem sie einen Delegaten erstellt, den Delegaten mit verschiedenen Parametern aufruft und schließlich die dynamische Methode mithilfe der Invoke Methode aufruft.
using System;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.VisualBasic;
public class Test
{
// Declare a delegate that will be used to execute the completed
// dynamic method.
private delegate int HelloInvoker(string msg, int ret);
public static void Main()
{
// Create an array that specifies the types of the parameters
// of the dynamic method. This method has a string parameter
// and an int parameter.
Type[] helloArgs = {typeof(string), typeof(int)};
// Create a dynamic method with the name "Hello", a return type
// of int, and two parameters whose types are specified by the
// array helloArgs. Create the method in the module that
// defines the Test class.
DynamicMethod hello = new DynamicMethod("Hello",
typeof(int),
helloArgs,
typeof(Test).Module);
// Create an array that specifies the parameter types of the
// overload of Console.WriteLine to be used in Hello.
Type[] writeStringArgs = {typeof(string)};
// Get the overload of Console.WriteLine that has one
// String parameter.
MethodInfo writeString =
typeof(Console).GetMethod("WriteLine", writeStringArgs);
// Get an ILGenerator and emit a body for the dynamic method.
ILGenerator il = hello.GetILGenerator();
// Load the first argument, which is a string, onto the stack.
il.Emit(OpCodes.Ldarg_0);
// Call the overload of Console.WriteLine that prints a string.
il.EmitCall(OpCodes.Call, writeString, null);
// The Hello method returns the value of the second argument;
// to do this, load the onto the stack and return.
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ret);
// Create a delegate that represents the dynamic method. This
// action completes the method, and any further attempts to
// change the method will cause an exception.
HelloInvoker hi =
(HelloInvoker) hello.CreateDelegate(typeof(HelloInvoker));
// Use the delegate to execute the dynamic method. Save and
// print the return value.
int retval = hi("\r\nHello, World!", 42);
Console.WriteLine("Executing delegate hi(\"Hello, World!\", 42) returned {0}",
retval);
// Do it again, with different arguments.
retval = hi("\r\nHi, Mom!", 5280);
Console.WriteLine("Executing delegate hi(\"Hi, Mom!\", 5280) returned {0}",
retval);
// Create an array of arguments to use with the Invoke method.
object[] invokeArgs = {"\r\nHello, World!", 42};
// Invoke the dynamic method using the arguments. This is much
// slower than using the delegate, because you must create an
// array to contain the arguments, and ValueType arguments
// must be boxed.
object objRet = hello.Invoke(null, invokeArgs);
Console.WriteLine("hello.Invoke returned {0}", objRet);
}
}
Imports System.Reflection
Imports System.Reflection.Emit
Public Class Test
' Declare a delegate that will be used to execute the completed
' dynamic method.
Private Delegate Function HelloInvoker(ByVal msg As String, _
ByVal ret As Integer) As Integer
Public Shared Sub Main()
' Create an array that specifies the types of the parameters
' of the dynamic method. This method has a String parameter
' and an Integer parameter.
Dim helloArgs() As Type = {GetType(String), GetType(Integer)}
' Create a dynamic method with the name "Hello", a return type
' of Integer, and two parameters whose types are specified by
' the array helloArgs. Create the method in the module that
' defines the Test class.
Dim hello As New DynamicMethod("Hello", _
GetType(Integer), _
helloArgs, _
GetType(Test).Module)
' Create an array that specifies the parameter types of the
' overload of Console.WriteLine to be used in Hello.
Dim writeStringArgs() As Type = {GetType(String)}
' Get the overload of Console.WriteLine that has one
' String parameter.
Dim writeString As MethodInfo = GetType(Console). _
GetMethod("WriteLine", writeStringArgs)
' Get an ILGenerator and emit a body for the dynamic method.
Dim il As ILGenerator = hello.GetILGenerator()
' Load the first argument, which is a string, onto the stack.
il.Emit(OpCodes.Ldarg_0)
' Call the overload of Console.WriteLine that prints a string.
il.EmitCall(OpCodes.Call, writeString, Nothing)
' The Hello method returns the value of the second argument;
' to do this, load the onto the stack and return.
il.Emit(OpCodes.Ldarg_1)
il.Emit(OpCodes.Ret)
' Create a delegate that represents the dynamic method. This
' action completes the method, and any further attempts to
' change the method will cause an exception.
Dim hi As HelloInvoker = _
hello.CreateDelegate(GetType(HelloInvoker))
' Use the delegate to execute the dynamic method. Save and
' print the return value.
Dim retval As Integer = hi(vbCrLf & "Hello, World!", 42)
Console.WriteLine("Executing delegate hi(""Hello, World!"", 42) returned " _
& retval)
' Do it again, with different arguments.
retval = hi(vbCrLf & "Hi, Mom!", 5280)
Console.WriteLine("Executing delegate hi(""Hi, Mom!"", 5280) returned " _
& retval)
' Create an array of arguments to use with the Invoke method.
Dim invokeArgs() As Object = {vbCrLf & "Hello, World!", 42}
' Invoke the dynamic method using the arguments. This is much
' slower than using the delegate, because you must create an
' array to contain the arguments, and ValueType arguments
' must be boxed. Note that this overload of Invoke is
' inherited from MethodBase, and simply calls the more
' complete overload of Invoke.
Dim objRet As Object = hello.Invoke(Nothing, invokeArgs)
Console.WriteLine("hello.Invoke returned " & objRet)
End Sub
End Class
' This code example produces the following output:
'
'Hello, World!
'Executing delegate hi("Hello, World!", 42) returned 42
'
'Hi, Mom!
'Executing delegate hi("Hi, Mom!", 5280) returned 5280
'
'Hello, World!
'hello.Invoke returned 42
'
Hinweise
Nachdem eine dynamische Methode abgeschlossen wurde, wird durch das Aufrufen der CreateDelegate-Methode oder der Invoke-Methode jeder weitere Versuch, MSIL hinzuzufügen, ignoriert. Es wird keine Ausnahme ausgelöst.
Note
Es gibt Einschränkungen für unverifizierbaren Code in dynamischen Methoden, auch in einigen voll vertrauenswürdigen Szenarien. Weitere Informationen finden Sie im Abschnitt "Verifizierung" in den Hinweisen für DynamicMethod.