TypeBuilder.DefineMethodOverride(MethodInfo, MethodInfo) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
지정된 메서드 선언을 구현하는 지정된 메서드 본문을 지정하며, 잠재적으로 다른 이름을 사용합니다.
public:
void DefineMethodOverride(System::Reflection::MethodInfo ^ methodInfoBody, System::Reflection::MethodInfo ^ methodInfoDeclaration);
public void DefineMethodOverride(System.Reflection.MethodInfo methodInfoBody, System.Reflection.MethodInfo methodInfoDeclaration);
member this.DefineMethodOverride : System.Reflection.MethodInfo * System.Reflection.MethodInfo -> unit
Public Sub DefineMethodOverride (methodInfoBody As MethodInfo, methodInfoDeclaration As MethodInfo)
매개 변수
- methodInfoBody
- MethodInfo
사용할 메서드 본문입니다. 개체여야 MethodBuilder 합니다.
- methodInfoDeclaration
- MethodInfo
선언을 사용할 메서드입니다.
예외
methodInfoBody 이 클래스에 속하지 않습니다.
methodInfoBody 또는 methodInfoDeclaration .입니다 null.
예제
다음 코드 예제에는 메서드I가 있는 인터페이스, 인터페이스 M() 를 구현하는 기본 클래스 A 및 기본 클래스 C 구현을 재정의 M() 하고 별도의 명시적 구현I.M()을 제공하는 파생 클래스가 포함되어 있습니다.
코드 예제의 메서드는 main() 파생 클래스 C를 내보내는 방법을 보여줍니다. 재정의 A.M() 는 단순히 동일한 시그니처를 사용하여 메서드 M() 를 내보내서 수행됩니다. 그러나 별도의 구현I.M()을 제공하려면 메서드 본문을 정의한 다음 메서드를 사용하여 DefineMethodOverride 해당 메서드 본문을 나타내는 MethodInfo메서드와 I.M() 연결해야 합니다. 메서드 본문의 이름은 중요하지 않습니다.
코드 예제에서는 내보낸 클래스의 인스턴스를 만듭니다. 개체를 MethodInfoI.M()가져오고 이를 사용하여 내보낸 클래스의 명시적 인터페이스 구현을 호출합니다. 그런 다음, 개체를 MethodInfo가져와 A.M() 서 해당 메서드의 내보낸 클래스 재정의를 호출하는 데 사용합니다.
using System;
using System.Reflection;
using System.Reflection.Emit;
public interface I
{
void M();
}
public class A
{
public virtual void M() { Console.WriteLine("In method A.M"); }
}
// The object of this code example is to emit code equivalent to
// the following C# code:
//
public class C : A, I
{
public override void M()
{
Console.WriteLine("Overriding A.M from C.M");
}
// In order to provide a different implementation from C.M when
// emitting the following explicit interface implementation,
// it is necessary to use a MethodImpl.
//
void I.M()
{
Console.WriteLine("The I.M implementation of C");
}
}
class Test
{
static void Main()
{
string name = "DefineMethodOverrideExample";
AssemblyName asmName = new AssemblyName(name);
AssemblyBuilder ab =
AssemblyBuilder.DefineDynamicAssembly(
asmName, AssemblyBuilderAccess.Run);
ModuleBuilder mb = ab.DefineDynamicModule(name);
TypeBuilder tb =
mb.DefineType("C", TypeAttributes.Public, typeof(A));
tb.AddInterfaceImplementation(typeof(I));
// Build the method body for the explicit interface
// implementation. The name used for the method body
// can be anything. Here, it is the name of the method,
// qualified by the interface name.
//
MethodBuilder mbIM = tb.DefineMethod("I.M",
MethodAttributes.Private | MethodAttributes.HideBySig |
MethodAttributes.NewSlot | MethodAttributes.Virtual |
MethodAttributes.Final,
null,
Type.EmptyTypes);
ILGenerator il = mbIM.GetILGenerator();
il.Emit(OpCodes.Ldstr, "The I.M implementation of C");
il.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine",
new Type[] { typeof(string) }));
il.Emit(OpCodes.Ret);
// DefineMethodOverride is used to associate the method
// body with the interface method that is being implemented.
//
tb.DefineMethodOverride(mbIM, typeof(I).GetMethod("M"));
MethodBuilder mbM = tb.DefineMethod("M",
MethodAttributes.Public | MethodAttributes.ReuseSlot |
MethodAttributes.Virtual | MethodAttributes.HideBySig,
null,
Type.EmptyTypes);
il = mbM.GetILGenerator();
il.Emit(OpCodes.Ldstr, "Overriding A.M from C.M");
il.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine",
new Type[] { typeof(string) }));
il.Emit(OpCodes.Ret);
Type tc = tb.CreateType();
object test = Activator.CreateInstance(tc);
MethodInfo mi = typeof(I).GetMethod("M");
mi.Invoke(test, null);
mi = typeof(A).GetMethod("M");
mi.Invoke(test, null);
}
}
/* This code example produces the following output:
The I.M implementation of C
Overriding A.M from C.M
*/
Imports System.Reflection
Imports System.Reflection.Emit
Public Interface I
Sub M()
End Interface
Public Class A
Public Overridable Sub M()
Console.WriteLine("In method A.M")
End Sub
End Class
' The object of this code example is to emit code equivalent to
' the following C# code:
'
Public Class C
Inherits A
Implements I
Public Overrides Sub M()
Console.WriteLine("Overriding A.M from C.M")
End Sub
' In order to provide a different implementation from C.M when
' emitting the following explicit interface implementation,
' it is necessary to use a MethodImpl.
'
Private Sub IM() Implements I.M
Console.WriteLine("The I.M implementation of C")
End Sub
End Class
Class Test
Shared Sub Main()
Dim name As String = "DefineMethodOverrideExample"
Dim asmName As New AssemblyName(name)
Dim ab As AssemblyBuilder = _
AssemblyBuilder.DefineDynamicAssembly( _
asmName, AssemblyBuilderAccess.Run)
Dim mb As ModuleBuilder = _
ab.DefineDynamicModule(name)
Dim tb As TypeBuilder = _
mb.DefineType("C", TypeAttributes.Public, GetType(A))
tb.AddInterfaceImplementation(GetType(I))
' Build the method body for the explicit interface
' implementation. The name used for the method body
' can be anything. Here, it is the name of the method,
' qualified by the interface name.
'
Dim mbIM As MethodBuilder = _
tb.DefineMethod("I.M", _
MethodAttributes.Private Or MethodAttributes.HideBySig Or _
MethodAttributes.NewSlot Or MethodAttributes.Virtual Or _
MethodAttributes.Final, _
Nothing, _
Type.EmptyTypes)
Dim il As ILGenerator = mbIM.GetILGenerator()
il.Emit(OpCodes.Ldstr, "The I.M implementation of C")
il.Emit(OpCodes.Call, GetType(Console).GetMethod("WriteLine", _
New Type() {GetType(String)}))
il.Emit(OpCodes.Ret)
' DefineMethodOverride is used to associate the method
' body with the interface method that is being implemented.
'
tb.DefineMethodOverride(mbIM, GetType(I).GetMethod("M"))
Dim mbM As MethodBuilder = tb.DefineMethod("M", _
MethodAttributes.Public Or MethodAttributes.ReuseSlot Or _
MethodAttributes.Virtual Or MethodAttributes.HideBySig, _
Nothing, _
Type.EmptyTypes)
il = mbM.GetILGenerator()
il.Emit(OpCodes.Ldstr, "Overriding A.M from C.M")
il.Emit(OpCodes.Call, GetType(Console).GetMethod("WriteLine", _
New Type() {GetType(String)}))
il.Emit(OpCodes.Ret)
Dim tc As Type = tb.CreateType()
Dim test As Object = Activator.CreateInstance(tc)
Dim mi As MethodInfo = GetType(I).GetMethod("M")
mi.Invoke(test, Nothing)
mi = GetType(A).GetMethod("M")
mi.Invoke(test, Nothing)
End Sub
End Class
' This code example produces the following output:
'
'The I.M implementation of C
'Overriding A.M from C.M
'
설명
메서드 재정의 또는 인터페이스 구현을 내보내려면 이 메서드를 사용하지 마세요. 기본 클래스의 메서드를 재정의하거나 인터페이스의 메서드를 구현하려면 코드 예제에 설명된 대로 재정의하거나 구현할 메서드와 동일한 이름과 시그니처를 가진 메서드를 내보내기만 하면 됩니다.
DefineMethodOverride 메서드 본문과 메서드 선언의 이름이 다를 때 메서드가 사용됩니다. 예를 들어 클래스는 기본 클래스 메서드를 재정의하고 코드 예제에 설명된 대로 이름이 같은 인터페이스 멤버에 대해 별도의 구현을 제공할 수도 있습니다.
Overridable Visual Basic). 형식에 의해 구현된 인터페이스, 파생 클래스의 메서드 또는 형식에 정의된 메서드에 정의된 메서드에 대해 선언을 수행할 수 있습니다. 선언이 인터페이스에만 있는 경우 인터페이스에 대해 정의된 슬롯이 변경됩니다. 기본 형식의 메서드에 대한 선언이 이루어지면 메서드의 슬롯이 재정의되고 재정의된 메서드에 대한 중복 항목도 바뀝니다. 재정의된 메서드는 선언된 실제 메서드일 수 없습니다. 메서드가 동일한 형식에 있는 경우 슬롯이 대체되고 대체된 메서드에 대한 중복 항목이 재정의됩니다.
메모
메서드 임프 MethodImpl 에 대한 자세한 내용은 ECMA C# 및 공용 언어 인프라 표준 및표준 ECMA-335 - CLI(공용 언어 인프라)의 ECMA 파티션 II 메타데이터 설명서를 참조하세요.
Important
메서드를 호출한 DefineMethodOverride 후에는 일부 기능을 methodInfoBody 변경할 수 없습니다. 예를 들어 메서드를 사용하여 methodInfoBody 제네릭 형식 매개 변수 SetGenericParameterAttributes 에 특성을 적용할 수 없습니다. 메서드를 DefineMethodOverride 사용해야 하는 경우 모든 특성이 정의된 후에 이 작업을 수행합니다 methodInfoBody .