MethodBuilder 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
동적 클래스의 메서드(또는 생성자)를 정의하고 나타냅니다.
public ref class MethodBuilder sealed : System::Reflection::MethodInfo, System::Runtime::InteropServices::_MethodBuilder
public ref class MethodBuilder sealed : 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
public sealed class MethodBuilder : System.Reflection.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
type MethodBuilder = class
inherit MethodInfo
Public NotInheritable Class MethodBuilder
Inherits MethodInfo
Implements _MethodBuilder
Public NotInheritable Class MethodBuilder
Inherits MethodInfo
- 상속
- 특성
- 구현
예제
다음 예제에서는 클래스를 MethodBuilder 사용하여 동적 형식 내에서 메서드를 만듭니다.
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 비고를 참조하세요.
속성
| Name | Description |
|---|---|
| Attributes |
이 메서드의 특성을 검색합니다. |
| CallingConvention |
메서드의 호출 규칙을 반환합니다. |
| ContainsGenericParameters |
이 형식에는 지원되지 않습니다. |
| CustomAttributes |
이 멤버의 사용자 지정 특성을 포함하는 컬렉션을 가져옵니다. (다음에서 상속됨 MemberInfo) |
| DeclaringType |
이 메서드를 선언하는 형식을 반환합니다. |
| InitLocals |
이 메서드의 지역 변수가 0으로 초기화되는지 여부를 지정하는 부울 값을 가져오거나 설정합니다. 이 속성의 기본값은 |
| IsAbstract |
메서드가 추상인지 여부를 나타내는 값을 가져옵니다. (다음에서 상속됨 MethodBase) |
| IsAssembly |
이 메서드 또는 생성자의 Assembly잠재적 표시 여부를 나타내는 값을 가져옵니다. 즉, 메서드 또는 생성자가 동일한 어셈블리의 다른 형식에 가장 많이 표시되며 어셈블리 외부의 파생 형식에는 표시되지 않습니다. (다음에서 상속됨 MethodBase) |
| IsConstructedGenericMethod |
동적 클래스의 메서드(또는 생성자)를 정의하고 나타냅니다. |
| IsConstructor |
메서드가 생성자인지 여부를 나타내는 값을 가져옵니다. (다음에서 상속됨 MethodBase) |
| IsFamily |
이 메서드 또는 생성자의 Family표시 여부를 나타내는 값을 가져옵니다. 즉, 메서드 또는 생성자가 해당 클래스 및 파생 클래스 내에서만 표시됩니다. (다음에서 상속됨 MethodBase) |
| IsFamilyAndAssembly |
이 메서드 또는 생성자의 FamANDAssem표시 여부를 나타내는 값을 가져옵니다. 즉, 메서드 또는 생성자는 파생 클래스에서 호출할 수 있지만 동일한 어셈블리에 있는 경우에만 호출할 수 있습니다. (다음에서 상속됨 MethodBase) |
| IsFamilyOrAssembly |
이 메서드 또는 생성자의 FamORAssem잠재적 표시 여부를 나타내는 값을 가져옵니다. 즉, 메서드 또는 생성자가 어디에 있든 파생 클래스 및 동일한 어셈블리의 클래스에서 호출할 수 있습니다. (다음에서 상속됨 MethodBase) |
| IsFinal |
이 메서드 |
| IsGenericMethod |
메서드가 제네릭 메서드인지 여부를 나타내는 값을 가져옵니다. |
| IsGenericMethodDefinition |
현재 MethodBuilder 개체가 제네릭 메서드의 정의를 나타내는지 여부를 나타내는 값을 가져옵니다. |
| IsHideBySig |
정확히 동일한 시그니처를 가진 동일한 종류의 멤버만 파생 클래스에 숨겨져 있는지 여부를 나타내는 값을 가져옵니다. (다음에서 상속됨 MethodBase) |
| IsPrivate |
이 멤버가 프라이빗인지 여부를 나타내는 값을 가져옵니다. (다음에서 상속됨 MethodBase) |
| IsPublic |
public 메서드인지 여부를 나타내는 값을 가져옵니다. (다음에서 상속됨 MethodBase) |
| IsSecurityCritical |
모든 경우에 a를 NotSupportedException throw합니다. |
| IsSecurityCritical |
현재 메서드 또는 생성자가 현재 신뢰 수준에서 보안에 중요하거나 보안이 안전한지 여부를 나타내는 값을 가져오므로 중요한 작업을 수행할 수 있습니다. (다음에서 상속됨 MethodBase) |
| IsSecuritySafeCritical |
모든 경우에 a를 NotSupportedException throw합니다. |
| IsSecuritySafeCritical |
현재 메서드 또는 생성자가 현재 신뢰 수준에서 보안에 중요한지 여부를 나타내는 값을 가져옵니다. 즉, 중요한 작업을 수행할 수 있고 투명 코드로 액세스할 수 있는지 여부입니다. (다음에서 상속됨 MethodBase) |
| IsSecurityTransparent |
모든 경우에 a를 NotSupportedException throw합니다. |
| IsSecurityTransparent |
현재 메서드 또는 생성자가 현재 신뢰 수준에서 투명하므로 중요한 작업을 수행할 수 없는지 여부를 나타내는 값을 가져옵니다. (다음에서 상속됨 MethodBase) |
| IsSpecialName |
이 메서드에 특별한 이름이 있는지 여부를 나타내는 값을 가져옵니다. (다음에서 상속됨 MethodBase) |
| IsStatic |
메서드 |
| IsVirtual |
메서드 |
| MemberType |
이 멤버가 MemberTypes 메서드임을 나타내는 값을 가져옵니다. (다음에서 상속됨 MethodInfo) |
| MetadataToken |
메타데이터 요소를 식별하는 값을 가져옵니다. (다음에서 상속됨 MemberInfo) |
| MethodHandle |
메서드의 내부 핸들을 검색합니다. 이 핸들을 사용하여 기본 메타데이터 핸들에 액세스합니다. |
| MethodImplementationFlags |
메서드 구현의 MethodImplAttributes 특성을 지정하는 플래그를 가져옵니다. (다음에서 상속됨 MethodBase) |
| Module |
현재 메서드가 정의되는 모듈을 가져옵니다. |
| Name |
이 메서드의 이름을 검색합니다. |
| ReflectedType |
리플렉션에 사용된 클래스를 검색하여 이 개체를 가져옵니다. |
| ReturnParameter |
ParameterInfo 반환 형식에 사용자 지정 한정자가 있는지 여부와 같이 메서드의 반환 형식에 대한 정보가 포함된 개체를 가져옵니다. |
| ReturnType |
이 MethodBuilder메서드가 나타내는 메서드의 반환 형식을 가져옵니다. |
| ReturnType |
이 메서드의 반환 형식을 가져옵니다. (다음에서 상속됨 MethodInfo) |
| ReturnTypeCustomAttributes |
메서드 반환 형식의 사용자 지정 특성을 반환합니다. |
| Signature |
메서드의 서명을 검색합니다. |
메서드
| Name | Description |
|---|---|
| AddDeclarativeSecurity(SecurityAction, PermissionSet) |
이 메서드에 선언적 보안을 추가합니다. |
| CreateDelegate(Type, Object) |
이 메서드에서 지정된 대상을 사용하여 지정된 형식의 대리자를 만듭니다. (다음에서 상속됨 MethodInfo) |
| CreateDelegate(Type) |
이 메서드에서 지정된 형식의 대리자를 만듭니다. (다음에서 상속됨 MethodInfo) |
| CreateMethodBody(Byte[], Int32) |
제공된 MSIL(Microsoft 중간 언어) 명령의 바이트 배열을 사용하여 메서드의 본문을 만듭니다. |
| DefineGenericParameters(String[]) |
현재 메서드의 제네릭 형식 매개 변수 수를 설정하고 이름을 지정하며 제약 조건을 정의하는 데 사용할 수 있는 개체 배열 GenericTypeParameterBuilder 을 반환합니다. |
| DefineParameter(Int32, ParameterAttributes, String) |
매개 변수 특성과 이 메서드의 매개 변수 이름 또는 이 메서드의 반환 값을 설정합니다. 사용자 지정 특성을 적용하는 데 사용할 수 있는 ParameterBuilder를 반환합니다. |
| Equals(Object) |
지정된 개체가 이 인스턴스와 같은지 여부를 확인합니다. |
| GetBaseDefinition() |
메서드의 기본 구현을 반환합니다. |
| GetCustomAttributes(Boolean) |
이 메서드에 대해 정의된 모든 사용자 지정 특성을 반환합니다. |
| GetCustomAttributes(Type, Boolean) |
지정된 형식으로 식별되는 사용자 지정 특성을 반환합니다. |
| GetCustomAttributesData() |
대상 멤버에 적용된 특성에 대한 데이터를 나타내는 개체 목록을 CustomAttributeData 반환합니다. (다음에서 상속됨 MemberInfo) |
| GetGenericArguments() |
제네릭인 경우 메서드의 GenericTypeParameterBuilder 형식 매개 변수를 나타내는 개체 배열을 반환합니다. |
| GetGenericMethodDefinition() |
이 메서드를 반환합니다. |
| GetHashCode() |
이 메서드의 해시 코드를 가져옵니다. |
| GetILGenerator() |
기본 MSIL(Microsoft 중간 언어) 스트림 크기가 64바이트인 이 메서드에 대한 |
| GetILGenerator(Int32) |
지정된 MSIL(Microsoft 중간 언어) 스트림 크기를 사용하여 이 메서드에 대한 |
| GetMethodBody() |
파생 클래스에서 재정의되는 경우 현재 메서드에 대한 MSIL 스트림, 지역 변수 및 예외에 대한 액세스를 제공하는 개체를 가져옵니다 MethodBody . (다음에서 상속됨 MethodBase) |
| GetMethodImplementationFlags() |
메서드의 구현 플래그를 반환합니다. |
| GetModule() |
이 메서드를 포함하는 모듈에 대한 참조를 반환합니다. |
| GetParameters() |
이 메서드의 매개 변수를 반환합니다. |
| GetToken() |
이 메서드의 |
| GetType() |
메서드의 특성을 검색하고 메서드 메타데이터에 대한 액세스를 제공합니다. (다음에서 상속됨 MethodInfo) |
| HasSameMetadataDefinitionAs(MemberInfo) |
동적 클래스의 메서드(또는 생성자)를 정의하고 나타냅니다. (다음에서 상속됨 MemberInfo) |
| Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) |
지정된 매개 변수를 따라 지정된 바인더의 제약 조건 아래를 전달하여 지정된 개체에서 이 인스턴스에 의해 반영된 메서드를 동적으로 호출합니다. |
| Invoke(Object, Object[]) |
지정된 매개 변수를 사용하여 현재 인스턴스가 나타내는 메서드 또는 생성자를 호출합니다. (다음에서 상속됨 MethodInfo) |
| IsDefined(Type, Boolean) |
지정된 사용자 지정 특성 형식이 정의되어 있는지 확인합니다. |
| MakeGenericMethod(Type[]) |
지정된 제네릭 형식 인수를 사용하여 현재 제네릭 메서드 정의에서 생성된 제네릭 메서드를 반환합니다. |
| MemberwiseClone() |
현재 Object단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
| SetCustomAttribute(ConstructorInfo, Byte[]) |
지정된 사용자 지정 특성 Blob을 사용하여 사용자 지정 특성을 설정합니다. |
| SetCustomAttribute(CustomAttributeBuilder) |
사용자 지정 특성 작성기를 사용하여 사용자 지정 특성을 설정합니다. |
| SetImplementationFlags(MethodImplAttributes) |
이 메서드의 구현 플래그를 설정합니다. |
| SetMarshal(UnmanagedMarshal) |
사용되지 않음.
이 메서드의 반환 형식에 대한 마샬링 정보를 설정합니다. |
| SetMethodBody(Byte[], Int32, Byte[], IEnumerable<ExceptionHandler>, IEnumerable<Int32>) |
MSIL(Microsoft 중간 언어) 명령의 지정된 바이트 배열을 사용하여 메서드의 본문을 만듭니다. |
| SetParameters(Type[]) |
메서드의 매개 변수 수와 형식을 설정합니다. |
| SetReturnType(Type) |
메서드의 반환 형식을 설정합니다. |
| SetSignature(Type, Type[], Type[], Type[], Type[][], Type[][]) |
반환 형식, 매개 변수 형식, 반환 형식 및 매개 변수 형식의 필수 및 선택적 사용자 지정 한정자를 포함하여 메서드 시그니처를 설정합니다. |
| SetSymCustomAttribute(String, Byte[]) |
Blob을 사용하여 기호화된 사용자 지정 특성을 설정합니다. |
| ToString() |
이 |
명시적 인터페이스 구현
확장명 메서드
| Name | Description |
|---|---|
| GetCustomAttribute(MemberInfo, Type, Boolean) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성을 검색하고 필요에 따라 해당 멤버의 상위 항목을 검사합니다. |
| GetCustomAttribute(MemberInfo, Type) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성을 검색합니다. |
| GetCustomAttribute<T>(MemberInfo, Boolean) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성을 검색하고 필요에 따라 해당 멤버의 상위 항목을 검사합니다. |
| GetCustomAttribute<T>(MemberInfo) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성을 검색합니다. |
| GetCustomAttributes(MemberInfo, Boolean) |
지정된 멤버에 적용되는 사용자 지정 특성의 컬렉션을 검색하고 필요에 따라 해당 멤버의 상위 항목을 검사합니다. |
| GetCustomAttributes(MemberInfo, Type, Boolean) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성 컬렉션을 검색하고 필요에 따라 해당 멤버의 상위 항목을 검사합니다. |
| GetCustomAttributes(MemberInfo, Type) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성 컬렉션을 검색합니다. |
| GetCustomAttributes(MemberInfo) |
지정된 멤버에 적용되는 사용자 지정 특성의 컬렉션을 검색합니다. |
| GetCustomAttributes<T>(MemberInfo, Boolean) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성 컬렉션을 검색하고 필요에 따라 해당 멤버의 상위 항목을 검사합니다. |
| GetCustomAttributes<T>(MemberInfo) |
지정된 멤버에 적용되는 지정된 형식의 사용자 지정 특성 컬렉션을 검색합니다. |
| GetRuntimeBaseDefinition(MethodInfo) |
메서드가 처음 선언된 직접 또는 간접 기본 클래스에서 지정된 메서드를 나타내는 개체를 검색합니다. |
| IsDefined(MemberInfo, Type, Boolean) |
지정된 형식의 사용자 지정 특성이 지정된 멤버에 적용되는지 여부와 필요에 따라 상위 항목에 적용되는지 여부를 나타냅니다. |
| IsDefined(MemberInfo, Type) |
지정된 형식의 사용자 지정 특성이 지정된 멤버에 적용되는지 여부를 나타냅니다. |