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
、一个实现 接口的基类A
和一个替代 基类实现的M()
派生类C
,并提供 的I.M()
单独显式M()
实现。
main()
代码示例的 方法演示如何发出派生类 C
。 的替代 A.M()
只需发出具有相同签名的方法 M()
即可实现。 但是,若要提供 的 I.M()
单独实现,必须定义方法主体,然后使用 DefineMethodOverride 方法将该方法主体与 MethodInfo 表示 的 I.M()
关联。 方法正文的名称并不重要。
代码示例创建发出的 类的实例。 它获取 MethodInfo 的 I.M()
对象,并使用它来调用发出的类的显式接口实现。 然后,它获取 MethodInfo 的 A.M()
对象,并使用它调用发出的类对该方法的重写。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
public interface class I
{
void M();
};
public ref 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 ref class C : A, I
{
public:
virtual void M() override
{
Console::WriteLine("Overriding A.M from C.M");
}
private:
// In order to provide a different implementation from C.M when
// emitting the following explicit interface implementation,
// it is necessary to use a MethodImpl.
//
virtual void IM() sealed = I::M
{
Console::WriteLine("The I::M implementation of C");
}
};
void main()
{
String^ name = "DefineMethodOverrideExample";
AssemblyName^ asmName = gcnew AssemblyName(name);
AssemblyBuilder^ ab =
AppDomain::CurrentDomain->DefineDynamicAssembly(
asmName, AssemblyBuilderAccess::RunAndSave);
ModuleBuilder^ mb = ab->DefineDynamicModule(name, name + ".dll");
TypeBuilder^ tb =
mb->DefineType("C", TypeAttributes::Public, A::typeid);
tb->AddInterfaceImplementation(I::typeid);
// 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,
nullptr,
Type::EmptyTypes);
ILGenerator^ il = mbIM->GetILGenerator();
il->Emit(OpCodes::Ldstr, "The I.M implementation of C");
il->Emit(OpCodes::Call, Console::typeid->GetMethod("WriteLine",
gcnew array<Type^> { String::typeid }));
il->Emit(OpCodes::Ret);
// DefineMethodOverride is used to associate the method
// body with the interface method that is being implemented.
//
tb->DefineMethodOverride(mbIM, I::typeid->GetMethod("M"));
MethodBuilder^ mbM = tb->DefineMethod("M",
MethodAttributes::Public | MethodAttributes::ReuseSlot |
MethodAttributes::Virtual | MethodAttributes::HideBySig,
nullptr,
Type::EmptyTypes);
il = mbM->GetILGenerator();
il->Emit(OpCodes::Ldstr, "Overriding A.M from C.M");
il->Emit(OpCodes::Call, Console::typeid->GetMethod("WriteLine",
gcnew array<Type^> { String::typeid }));
il->Emit(OpCodes::Ret);
Type^ tc = tb->CreateType();
// Save the emitted assembly, to examine with Ildasm.exe.
ab->Save(name + ".dll");
Object^ test = Activator::CreateInstance(tc);
MethodInfo^ mi = I::typeid->GetMethod("M");
mi->Invoke(test, nullptr);
mi = A::typeid->GetMethod("M");
mi->Invoke(test, nullptr);
}
/* This code example produces the following output:
The I.M implementation of C
Overriding A.M from C.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 =
AppDomain.CurrentDomain.DefineDynamicAssembly(
asmName, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder mb = ab.DefineDynamicModule(name, name + ".dll");
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();
// Save the emitted assembly, to examine with Ildasm.exe.
ab.Save(name + ".dll");
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 = _
AppDomain.CurrentDomain.DefineDynamicAssembly( _
asmName, AssemblyBuilderAccess.RunAndSave)
Dim mb As ModuleBuilder = _
ab.DefineDynamicModule(name, name & ".dll")
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()
' Save the emitted assembly, to examine with Ildasm.exe.
ab.Save(name & ".dll")
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 方法正文和方法声明具有不同的名称时,将使用 方法。 例如,类可能会重写基类方法,并为具有相同名称的接口成员提供单独的实现,如代码示例所示。
DefineMethodOverride
定义一个 methodimpl
,它由一对元数据标记组成。 一个标记指向实现,另一个标记指向正文实现的声明。 必须在方法 impl 所定义的类型上定义正文,并且正文必须是 Visual Basic) Overridable
中的虚拟 (。 声明可以针对由 类型实现的接口上定义的方法、派生类上的方法或在 类型中定义的方法。 如果声明仅在接口上,则会更改为接口定义的槽。 如果对基类型上的方法进行声明,则该方法的槽将被重写,并替换重写方法的任何重复项。 重写的方法不能是声明的实际方法。 如果方法位于同一类型上,则替换槽,并重写所替换方法的任何重复项。
注意
有关方法 impls 的详细信息,请参阅 MethodImpl
ECMA C# 中的 ECMA 分区 II 元数据文档,以及公共语言基础结构标准和标准 ECMA-335 - 公共语言基础结构 (CLI) 。
重要
DefineMethodOverride调用 方法后,无法更改 的某些methodInfoBody
功能。 例如,不能使用 SetGenericParameterAttributes 方法将 特性应用于 的methodInfoBody
泛型类型参数。 如果必须使用 DefineMethodOverride 方法,请在定义 的所有特征 methodInfoBody
后执行此操作。