MethodBuilder 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義及表示動態類別上的方法 (或建構函式)。
public ref class MethodBuilder sealed : System::Reflection::MethodInfo
public ref class MethodBuilder abstract : System::Reflection::MethodInfo
public ref class MethodBuilder sealed : System::Reflection::MethodInfo, System::Runtime::InteropServices::_MethodBuilder
public sealed class MethodBuilder : System.Reflection.MethodInfo
public abstract class MethodBuilder : System.Reflection.MethodInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public sealed class MethodBuilder : System.Reflection.MethodInfo, System.Runtime.InteropServices._MethodBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class MethodBuilder : System.Reflection.MethodInfo, System.Runtime.InteropServices._MethodBuilder
type MethodBuilder = class
inherit MethodInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type MethodBuilder = class
inherit MethodInfo
interface _MethodBuilder
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MethodBuilder = class
inherit MethodInfo
interface _MethodBuilder
Public NotInheritable Class MethodBuilder
Inherits MethodInfo
Public MustInherit Class MethodBuilder
Inherits MethodInfo
Public NotInheritable Class MethodBuilder
Inherits MethodInfo
Implements _MethodBuilder
- 繼承
- 屬性
- 實作
範例
下列範例會 MethodBuilder 使用 類別在動態類型內建立方法。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
void AddMethodDynamically( TypeBuilder^ myTypeBld,
String^ mthdName,
array<Type^>^ mthdParams,
Type^ returnType,
String^ mthdAction )
{
MethodBuilder^ myMthdBld = myTypeBld->DefineMethod( mthdName, static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static), returnType, mthdParams );
ILGenerator^ ILOut = myMthdBld->GetILGenerator();
int numParams = mthdParams->Length;
for ( Byte x = 0; x < numParams; x++ )
{
ILOut->Emit( OpCodes::Ldarg_S, x );
}
if ( numParams > 1 )
{
for ( int y = 0; y < (numParams - 1); y++ )
{
if ( mthdAction->Equals( "A" ) )
ILOut->Emit( OpCodes::Add );
else
if ( mthdAction->Equals( "M" ) )
ILOut->Emit( OpCodes::Mul );
else
ILOut->Emit( OpCodes::Add );
}
}
ILOut->Emit( OpCodes::Ret );
};
void main()
{
AppDomain^ myDomain = AppDomain::CurrentDomain;
AssemblyName^ asmName = gcnew AssemblyName;
asmName->Name = "MyDynamicAsm";
AssemblyBuilder^ myAsmBuilder = myDomain->DefineDynamicAssembly( asmName,
AssemblyBuilderAccess::RunAndSave );
ModuleBuilder^ myModule = myAsmBuilder->DefineDynamicModule( "MyDynamicAsm",
"MyDynamicAsm.dll" );
TypeBuilder^ myTypeBld = myModule->DefineType( "MyDynamicType",
TypeAttributes::Public );
// Get info from the user to build the method dynamically.
Console::WriteLine( "Let's build a simple method dynamically!" );
Console::WriteLine( "Please enter a few numbers, separated by spaces." );
String^ inputNums = Console::ReadLine();
Console::Write( "Do you want to [A]dd (default) or [M]ultiply these numbers? " );
String^ myMthdAction = Console::ReadLine()->ToUpper();
Console::Write( "Lastly, what do you want to name your new dynamic method? " );
String^ myMthdName = Console::ReadLine();
// Process inputNums into an array and create a corresponding Type array
int index = 0;
array<String^>^inputNumsList = inputNums->Split();
array<Type^>^myMthdParams = gcnew array<Type^>(inputNumsList->Length);
array<Object^>^inputValsList = gcnew array<Object^>(inputNumsList->Length);
for each (String^ inputNum in inputNumsList)
{
inputValsList[ index ] = Convert::ToInt32( inputNum );
myMthdParams[ index ] = int::typeid;
index++;
}
// Now, call the method building method with the parameters, passing the
// TypeBuilder by reference.
AddMethodDynamically( myTypeBld,
myMthdName,
myMthdParams,
int::typeid,
myMthdAction );
Type^ myType = myTypeBld->CreateType();
Console::WriteLine( "---" );
Console::WriteLine( "The result of {0} the inputted values is: {1}",
((myMthdAction->Equals( "M" )) ? "multiplying" : "adding"),
myType->InvokeMember( myMthdName,
BindingFlags::InvokeMethod | BindingFlags::Public | BindingFlags::Static,
nullptr,
nullptr,
inputValsList ) );
Console::WriteLine( "---" );
// Let's take a look at the method we created.
// If you are interested in seeing the MSIL generated dynamically for the method
// your program generated, change to the directory where you ran the compiled
// code sample and type "ildasm MyDynamicAsm.dll" at the prompt. When the list
// of manifest contents appears, click on "MyDynamicType" and then on the name of
// of the method you provided during execution.
myAsmBuilder->Save( "MyDynamicAsm.dll" );
MethodInfo^ myMthdInfo = myType->GetMethod( myMthdName );
Console::WriteLine( "Your Dynamic Method: {0};", myMthdInfo );
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DemoMethodBuilder
{
public static void AddMethodDynamically (TypeBuilder myTypeBld,
string mthdName,
Type[] mthdParams,
Type returnType,
string mthdAction)
{
MethodBuilder myMthdBld = myTypeBld.DefineMethod(
mthdName,
MethodAttributes.Public |
MethodAttributes.Static,
returnType,
mthdParams);
ILGenerator ILout = myMthdBld.GetILGenerator();
int numParams = mthdParams.Length;
for (byte x=0; x < numParams; x++)
{
ILout.Emit(OpCodes.Ldarg_S, x);
}
if (numParams > 1)
{
for (int y=0; y<(numParams-1); y++)
{
switch (mthdAction)
{
case "A": ILout.Emit(OpCodes.Add);
break;
case "M": ILout.Emit(OpCodes.Mul);
break;
default: ILout.Emit(OpCodes.Add);
break;
}
}
}
ILout.Emit(OpCodes.Ret);
}
public static void Main()
{
AppDomain myDomain = AppDomain.CurrentDomain;
AssemblyName asmName = new AssemblyName();
asmName.Name = "MyDynamicAsm";
AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
asmName,
AssemblyBuilderAccess.RunAndSave);
ModuleBuilder myModule = myAsmBuilder.DefineDynamicModule("MyDynamicAsm",
"MyDynamicAsm.dll");
TypeBuilder myTypeBld = myModule.DefineType("MyDynamicType",
TypeAttributes.Public);
// Get info from the user to build the method dynamically.
Console.WriteLine("Let's build a simple method dynamically!");
Console.WriteLine("Please enter a few numbers, separated by spaces.");
string inputNums = Console.ReadLine();
Console.Write("Do you want to [A]dd (default) or [M]ultiply these numbers? ");
string myMthdAction = Console.ReadLine().ToUpper();
Console.Write("Lastly, what do you want to name your new dynamic method? ");
string myMthdName = Console.ReadLine();
// Process inputNums into an array and create a corresponding Type array
int index = 0;
string[] inputNumsList = inputNums.Split();
Type[] myMthdParams = new Type[inputNumsList.Length];
object[] inputValsList = new object[inputNumsList.Length];
foreach (string inputNum in inputNumsList)
{
inputValsList[index] = (object)Convert.ToInt32(inputNum);
myMthdParams[index] = typeof(int);
index++;
}
// Now, call the method building method with the parameters, passing the
// TypeBuilder by reference.
AddMethodDynamically(myTypeBld,
myMthdName,
myMthdParams,
typeof(int),
myMthdAction);
Type myType = myTypeBld.CreateType();
Console.WriteLine("---");
Console.WriteLine("The result of {0} the inputted values is: {1}",
((myMthdAction == "M") ? "multiplying" : "adding"),
myType.InvokeMember(myMthdName,
BindingFlags.InvokeMethod | BindingFlags.Public |
BindingFlags.Static,
null,
null,
inputValsList));
Console.WriteLine("---");
// Let's take a look at the method we created.
// If you are interested in seeing the MSIL generated dynamically for the method
// your program generated, change to the directory where you ran the compiled
// code sample and type "ildasm MyDynamicAsm.dll" at the prompt. When the list
// of manifest contents appears, click on "MyDynamicType" and then on the name of
// of the method you provided during execution.
myAsmBuilder.Save("MyDynamicAsm.dll");
MethodInfo myMthdInfo = myType.GetMethod(myMthdName);
Console.WriteLine("Your Dynamic Method: {0};", myMthdInfo.ToString());
}
}
Imports System.Reflection
Imports System.Reflection.Emit
Class DemoMethodBuilder
Public Shared Sub AddMethodDynamically(ByVal myTypeBld As TypeBuilder, _
ByVal mthdName As String, _
ByVal mthdParams() As Type, _
ByVal returnType As Type, _
ByVal mthdAction As String)
Dim myMthdBld As MethodBuilder = myTypeBld.DefineMethod(mthdName, _
MethodAttributes.Public Or MethodAttributes.Static, _
returnType, _
mthdParams)
Dim ILout As ILGenerator = myMthdBld.GetILGenerator()
Dim numParams As Integer = mthdParams.Length
Dim x As Byte
For x = 0 To numParams - 1
ILout.Emit(OpCodes.Ldarg_S, x)
Next x
If numParams > 1 Then
Dim y As Integer
For y = 0 To (numParams - 1) - 1
Select Case mthdAction
Case "A"
ILout.Emit(OpCodes.Add)
Case "M"
ILout.Emit(OpCodes.Mul)
Case Else
ILout.Emit(OpCodes.Add)
End Select
Next y
End If
ILout.Emit(OpCodes.Ret)
End Sub
Public Shared Sub Main()
Dim myDomain As AppDomain = AppDomain.CurrentDomain
Dim asmName As New AssemblyName()
asmName.Name = "MyDynamicAsm"
Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(asmName, _
AssemblyBuilderAccess.RunAndSave)
Dim myModule As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyDynamicAsm", _
"MyDynamicAsm.dll")
Dim myTypeBld As TypeBuilder = myModule.DefineType("MyDynamicType", TypeAttributes.Public)
' Get info from the user to build the method dynamically.
Console.WriteLine("Let's build a simple method dynamically!")
Console.WriteLine("Please enter a few numbers, separated by spaces.")
Dim inputNums As String = Console.ReadLine()
Console.Write("Do you want to [A]dd (default) or [M]ultiply these numbers? ")
Dim myMthdAction As String = Console.ReadLine().ToUpper()
Console.Write("Lastly, what do you want to name your new dynamic method? ")
Dim myMthdName As String = Console.ReadLine()
' Process inputNums into an array and create a corresponding Type array
Dim index As Integer = 0
Dim inputNumsList As String() = inputNums.Split()
Dim myMthdParams(inputNumsList.Length - 1) As Type
Dim inputValsList(inputNumsList.Length - 1) As Object
Dim inputNum As String
For Each inputNum In inputNumsList
inputValsList(index) = CType(Convert.ToInt32(inputNum), Object)
myMthdParams(index) = GetType(Integer)
index += 1
Next inputNum
' Now, call the method building method with the parameters, passing the
' TypeBuilder by reference.
AddMethodDynamically(myTypeBld, myMthdName, myMthdParams, GetType(Integer), myMthdAction)
Dim myType As Type = myTypeBld.CreateType()
Dim description as String
If myMthdAction = "M" Then
description = "multiplying"
Else
description = "adding"
End If
Console.WriteLine("---")
Console.WriteLine("The result of {0} the values is: {1}", _
description, _
myType.InvokeMember(myMthdName, _
BindingFlags.InvokeMethod _
Or BindingFlags.Public _
Or BindingFlags.Static, _
Nothing, _
Nothing, _
inputValsList))
Console.WriteLine("---")
' If you are interested in seeing the MSIL generated dynamically for the method
' your program generated, change to the directory where you ran the compiled
' code sample and type "ildasm MyDynamicAsm.dll" at the prompt. When the list
' of manifest contents appears, click on "MyDynamicType" and then on the name of
' of the method you provided during execution.
myAsmBuilder.Save("MyDynamicAsm.dll")
Dim myMthdInfo As MethodInfo = myType.GetMethod(myMthdName)
Console.WriteLine("Your Dynamic Method: {0};", myMthdInfo.ToString())
End Sub
End Class
備註
如需此 API 的詳細資訊,請參閱 MethodBuilder 的補充 API 備註。
建構函式
MethodBuilder() |
初始化 MethodBuilder 類別的新執行個體。 |
屬性
Attributes |
擷取此方法的屬性。 |
CallingConvention |
傳回方法的呼叫慣例。 |
ContainsGenericParameters |
現在支援這種類型。 |
ContainsGenericParameters |
取得值,指出泛型方法是否包含未指派的泛型型別參數。 (繼承來源 MethodInfo) |
CustomAttributes |
取得包含此成員之自訂屬性的集合。 (繼承來源 MemberInfo) |
DeclaringType |
傳回宣告這個方法的類型。 |
InitLocals |
取得或設定布林值,指定在這個方法中的區域變數是否以零起始。 此屬性的預設值為 |
InitLocalsCore |
在衍生類別中覆寫時,取得或設定值,指出這個方法中的局部變數是否為零初始化。 |
IsAbstract |
取得值,指出方法是否為抽象。 (繼承來源 MethodBase) |
IsAssembly |
取得值,指出 Assembly 是否描述此方法或建構函式 (Constructor) 的潛在可視性;亦即,最多只有相同組件 (Assembly) 中的其他型別可以看見該方法或建構函式,組件外部的衍生型別 (Derived Type) 則看不見它們。 (繼承來源 MethodBase) |
IsCollectible |
取得指出此 MemberInfo 物件是否為可回收 AssemblyLoadContext 中保存之組件一部分的值。 (繼承來源 MemberInfo) |
IsConstructedGenericMethod |
定義及表示動態類別上的方法 (或建構函式)。 |
IsConstructedGenericMethod |
定義及表示動態類別上的方法 (或建構函式)。 (繼承來源 MethodBase) |
IsConstructor |
取得值,指出方法是否為建構函示。 (繼承來源 MethodBase) |
IsFamily |
取得值,指出 Family 是否描述此方法或建構函式的可視性;亦即,您只能在其類別和衍生類別內看見該方法或建構函式。 (繼承來源 MethodBase) |
IsFamilyAndAssembly |
取得值,指出 FamANDAssem 是否描述此方法或建構函式的可視性;亦即,只有當該方法或建構函式位於相同的組件時,衍生類別才能呼叫它們。 (繼承來源 MethodBase) |
IsFamilyOrAssembly |
取得值,指出 FamORAssem 是否描述此方法或建構函式的潛在可視性;亦即,無論該方法或建構函式位於何處,衍生類別以及相同組件中的類別都可以呼叫它們。 (繼承來源 MethodBase) |
IsFinal |
取得值,指出這個方法是否為 |
IsGenericMethod |
取得可指出此方法是否為泛型方法的值。 |
IsGenericMethod |
取得值,指出目前的方法是否為泛型方法。 (繼承來源 MethodInfo) |
IsGenericMethodDefinition |
取得值,指出目前的 MethodBuilder 物件是否代表泛型方法的定義。 |
IsGenericMethodDefinition |
取得值,表示目前的 MethodInfo是否代表泛型方法的定義。 (繼承來源 MethodInfo) |
IsHideBySig |
取得值,指出是否只有簽章完全一樣的同類成員隱藏於衍生類別中。 (繼承來源 MethodBase) |
IsPrivate |
取得值,指出這個成員是否為私用的 (Private)。 (繼承來源 MethodBase) |
IsPublic |
取得值,指出這是否為公用的方法。 (繼承來源 MethodBase) |
IsSecurityCritical |
在所有情況下都擲回 NotSupportedException。 |
IsSecurityCritical |
取得值,這個值表示目前方法或建構函式在目前信任層級上是否為安全性關鍵或安全性安全關鍵,因而可以執行重要的作業。 (繼承來源 MethodBase) |
IsSecuritySafeCritical |
在所有情況下都擲回 NotSupportedException。 |
IsSecuritySafeCritical |
取得值,這個值表示目前方法或建構函式在目前信任層級上是否為安全性安全關鍵,也就是說,它是否可以執行重要作業並且可供透明程式碼存取。 (繼承來源 MethodBase) |
IsSecurityTransparent |
在所有情況下都擲回 NotSupportedException。 |
IsSecurityTransparent |
取得值,這個值表示目前方法或建構函式在目前信任層級上是否為透明,因此不得執行重要作業。 (繼承來源 MethodBase) |
IsSpecialName |
取得值,指出這個方法是否有特別的名稱。 (繼承來源 MethodBase) |
IsStatic |
取得值指出方法是否為 |
IsVirtual |
取得值指出方法是否為 |
MemberType |
取得 MemberTypes 值,表示這個成員為方法。 (繼承來源 MethodInfo) |
MetadataToken |
取得語彙基元,可識別中繼資料中的目前動態模組。 |
MetadataToken |
取得值,這個值可識別中繼資料項目。 (繼承來源 MemberInfo) |
MethodHandle |
擷取方法的內部控制代碼。 使用此控制代碼來存取基礎中繼資料控制代碼。 |
MethodHandle |
取得方法內部中繼資料 (Metadata) 表示的控制代碼。 (繼承來源 MethodBase) |
MethodImplementationFlags |
定義及表示動態類別上的方法 (或建構函式)。 |
MethodImplementationFlags |
取得 MethodImplAttributes 旗標,這些旗標會指定方法實作的屬性。 (繼承來源 MethodBase) |
Module |
取得所要定義之目前方法中的模組。 |
Module |
取得用於定義型別的模組,該型別宣告以目前 MemberInfo 表示的成員。 (繼承來源 MemberInfo) |
Name |
擷取這個方法的名稱。 |
ReflectedType |
取得用於反映中以取得方法的類別。 |
ReflectedType |
取得類別物件,是用來取得這個 |
ReturnParameter |
取得 ParameterInfo 物件,這個物件包含方法之傳回型別的相關資訊,例如傳回型別是否具有自訂修飾詞。 |
ReturnParameter |
取得 ParameterInfo 物件,這個物件包含方法之傳回型別的相關資訊,例如傳回型別是否具有自訂修飾詞。 (繼承來源 MethodInfo) |
ReturnType |
取得這個 MethodBuilder 所表示之方法的傳回型別。 |
ReturnType |
取得這個方法的傳回型別 (Return Type)。 (繼承來源 MethodInfo) |
ReturnTypeCustomAttributes |
傳回方法之傳回型別的自訂屬性。 |
ReturnTypeCustomAttributes |
取得傳回型別的自訂屬性。 (繼承來源 MethodInfo) |
Signature |
擷取方法的簽章。 |