MethodBuilder.CreateMethodBody(Byte[], Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立方法的主體,方法是使用 Microsoft Intermediate Language (MSIL) 指示所提供的位元組陣列。
public:
void CreateMethodBody(cli::array <System::Byte> ^ il, int count);
public void CreateMethodBody (byte[] il, int count);
member this.CreateMethodBody : byte[] * int -> unit
Public Sub CreateMethodBody (il As Byte(), count As Integer)
參數
- il
- Byte[]
包含有效 MSIL 指示的陣列。 如果這個參數是 null
,便會清除該方法的主體。
- count
- Int32
MSIL 陣列中的有效位元組數目。 如果 MSIL 是 null
,就會忽略這個值。
例外狀況
count
不在提供的 MSIL 指示陣列中的索引範圍內,且 il
不是 null
。
先前已使用 CreateType() 建立包含類型。
-或-
這個方法之前是在 MethodBuilder
物件上,以非 null
的 il
引數所呼叫。
-或-
針對於目前的方法,IsGenericMethod 屬性為 true
,但 IsGenericMethodDefinition 屬性為 false
。
範例
在下列範例中,使用 CreateMethodBody
透過opcode產生新增兩個整數的簡單方法。
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
class MethodBodyDemo
{
public:
// This class will demonstrate how to create a method body using
// the MethodBuilder::CreateMethodBody(Byte[], int) method.
static Type^ BuildDynType()
{
Type^ addType = nullptr;
AppDomain^ currentDom = Thread::GetDomain();
AssemblyName^ myAsmName = gcnew AssemblyName;
myAsmName->Name = "MyDynamicAssembly";
AssemblyBuilder^ myAsmBldr = currentDom->DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess::RunAndSave );
// The dynamic assembly space has been created. Next, create a module
// within it. The type Point will be reflected into this module.
ModuleBuilder^ myModuleBldr = myAsmBldr->DefineDynamicModule( "MyModule" );
TypeBuilder^ myTypeBldr = myModuleBldr->DefineType( "Adder" );
array<Type^>^temp0 = {int::typeid,int::typeid};
MethodBuilder^ myMthdBldr = myTypeBldr->DefineMethod( "DoAdd", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static), int::typeid, temp0 );
// Build the array of Bytes holding the MSIL instructions.
/* 02h is the opcode for ldarg.0 */
/* 03h is the opcode for ldarg.1 */
/* 58h is the opcode for add */
/* 2Ah is the opcode for ret */
array<Byte>^temp1 = {0x02,0x03,0x58,0x2A};
array<Byte>^ILcodes = temp1;
myMthdBldr->CreateMethodBody( ILcodes, ILcodes->Length );
addType = myTypeBldr->CreateType();
return addType;
}
};
int main()
{
Type^ myType = MethodBodyDemo::BuildDynType();
Console::WriteLine( "---" );
Console::Write( "Enter the first integer to add: " );
int aVal = Convert::ToInt32( Console::ReadLine() );
Console::Write( "Enter the second integer to add: " );
int bVal = Convert::ToInt32( Console::ReadLine() );
Object^ adderInst = Activator::CreateInstance( myType, gcnew array<Object^>(0) );
array<Object^>^temp1 = {aVal,bVal};
Console::WriteLine( "The value of adding {0} to {1} is: {2}.", aVal, bVal, myType->InvokeMember( "DoAdd", BindingFlags::InvokeMethod, nullptr, adderInst, temp1 ) );
}
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
class MethodBodyDemo {
// This class will demonstrate how to create a method body using
// the MethodBuilder.CreateMethodBody(byte[], int) method.
public static Type BuildDynType() {
Type addType = null;
AppDomain currentDom = Thread.GetDomain();
AssemblyName myAsmName = new AssemblyName();
myAsmName.Name = "MyDynamicAssembly";
AssemblyBuilder myAsmBldr = currentDom.DefineDynamicAssembly(
myAsmName,
AssemblyBuilderAccess.RunAndSave);
// The dynamic assembly space has been created. Next, create a module
// within it. The type Point will be reflected into this module.
ModuleBuilder myModuleBldr = myAsmBldr.DefineDynamicModule("MyModule");
TypeBuilder myTypeBldr = myModuleBldr.DefineType("Adder");
MethodBuilder myMthdBldr = myTypeBldr.DefineMethod("DoAdd",
MethodAttributes.Public |
MethodAttributes.Static,
typeof(int),
new Type[]
{typeof(int), typeof(int)});
// Build the array of Bytes holding the MSIL instructions.
byte[] ILcodes = new byte[] {
0x02, /* 02h is the opcode for ldarg.0 */
0x03, /* 03h is the opcode for ldarg.1 */
0x58, /* 58h is the opcode for add */
0x2A /* 2Ah is the opcode for ret */
};
myMthdBldr.CreateMethodBody(ILcodes, ILcodes.Length);
addType = myTypeBldr.CreateType();
return addType;
}
public static void Main() {
Type myType = BuildDynType();
Console.WriteLine("---");
Console.Write("Enter the first integer to add: ");
int aVal = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second integer to add: ");
int bVal = Convert.ToInt32(Console.ReadLine());
object adderInst = Activator.CreateInstance(myType, new object[0]);
Console.WriteLine("The value of adding {0} to {1} is: {2}.",
aVal, bVal,
myType.InvokeMember("DoAdd",
BindingFlags.InvokeMethod,
null,
adderInst,
new object[] {aVal, bVal}));
}
}
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
_
Class MethodBodyDemo
' This class will demonstrate how to create a method body using
' the MethodBuilder.CreateMethodBody(byte[], int) method.
Public Shared Function BuildDynType() As Type
Dim addType As Type = Nothing
Dim currentDom As AppDomain = Thread.GetDomain()
Dim myAsmName As New AssemblyName()
myAsmName.Name = "MyDynamicAssembly"
Dim myAsmBldr As AssemblyBuilder = currentDom.DefineDynamicAssembly(myAsmName, _
AssemblyBuilderAccess.RunAndSave)
' The dynamic assembly space has been created. Next, create a module
' within it. The type Point will be reflected into this module.
Dim myModuleBldr As ModuleBuilder = myAsmBldr.DefineDynamicModule("MyModule")
Dim myTypeBldr As TypeBuilder = myModuleBldr.DefineType("Adder")
Dim myMthdBldr As MethodBuilder = myTypeBldr.DefineMethod("DoAdd", _
MethodAttributes.Public Or MethodAttributes.Static, _
GetType(Integer), _
New Type() {GetType(Integer), GetType(Integer)})
' Build the array of Bytes holding the MSIL instructions.
Dim ILcodes() As Byte = {&H2, &H3, &H58, &H2A}
' 02h is the opcode for ldarg.0
' 03h is the opcode for ldarg.1
' 58h is the opcode for add
' 2Ah is the opcode for ret
myMthdBldr.CreateMethodBody(ILcodes, ILcodes.Length)
addType = myTypeBldr.CreateType()
Return addType
End Function 'BuildDynType
Public Shared Sub Main()
Dim myType As Type = BuildDynType()
Console.WriteLine("---")
Console.Write("Enter the first integer to add: ")
Dim aVal As Integer = Convert.ToInt32(Console.ReadLine())
Console.Write("Enter the second integer to add: ")
Dim bVal As Integer = Convert.ToInt32(Console.ReadLine())
Dim adderInst As Object = Activator.CreateInstance(myType, New Object() {})
Console.WriteLine("The value of adding {0} to {1} is: {2}.", _
aVal, bVal, _
myType.InvokeMember("DoAdd", _
BindingFlags.InvokeMethod, _
Nothing, _
adderInst, _
New Object() {aVal, bVal}))
End Sub
End Class
備註
這個方法會從 il
建立方法的主體,此數位包含 MSIL 指令做為 opcode。 有效 MSIL 的位元組數目是由計數來指定。
注意
目前並不完全支援此功能。 用戶無法提供令牌修正和例外狀況處理程式的位置。