This documentation is archived and is not being maintained.

TypeBuilder.DefineMethodOverride Method

Specifies a given method body that implements a given method declaration, potentially with a different name.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

member DefineMethodOverride : 
        methodInfoBody:MethodInfo * 
        methodInfoDeclaration:MethodInfo -> unit 


Type: System.Reflection.MethodInfo
The method body to be used. This should be a MethodBuilder object.
Type: System.Reflection.MethodInfo
The method whose declaration is to be used.


methodInfoBody does not belong to this class.


methodInfoBody or methodInfoDeclaration is a null reference (Nothing in Visual Basic).


The type was previously created using CreateType.


The declaring type of methodInfoBody is not the type represented by this TypeBuilder.

Do not use this method to emit method overrides or interface implementations. To override a method of a base class or to implement a method of an interface, simply emit a method with the same name and signature as the method to be overridden or implemented, as demonstrated in the code example.

The DefineMethodOverride method is used when a method body and a method declaration have different names. For example, a class might override a base class method and also provide a separate implementation for an interface member with the same name, as demonstrated in the code example.

DefineMethodOverride defines a methodimpl, which consists of a pair of metadata tokens. One token points to an implementation, and the other token points to a declaration that the body implements. The body must be defined on the type the method impl is defined on, and the body must be virtual (Overridable in Visual Basic). The declaration can be made to a method defined on an interface implemented by the type, a method on a derived class, or a method defined in the type. If the declaration is on an interface only, the slot defined for the interface is altered. If the declaration is made to a method on a base type, the slot for the method is overridden and any duplicates for the overridden method are also replaced. The overridden method cannot be the actual method that is declared. If the method is on the same type, the slot is replaced and any duplicates for the replaced methods are overridden.


For more information about method impls, see MethodImpl in the ECMA Partition II Metadata documentation. The documentation is available online; see ECMA C# and Common Language Infrastructure Standards on MSDN and Standard ECMA-335 - Common Language Infrastructure (CLI) on the Ecma International Web site.

Important noteImportant

After the DefineMethodOverride method is called, some features of methodInfoBody cannot be changed. For example, you cannot apply an attribute to a generic type parameter of methodInfoBody by using the SetGenericParameterAttributes method. If you must use the DefineMethodOverride method, do so after all characteristics of methodInfoBody have been defined.

The following code example contains an interface I with a method M(), a base class A that implements the interface, and a derived class C that overrides the base class implementation of M() and also provides a separate explicit implementation of I.M().

The main() method of the code example shows how to emit the derived class C. The override of A.M() is accomplished simply by emitting a method M() with the same signature. However, to provide a separate implementation of I.M(), you must define a method body and then use the DefineMethodOverride method to associate that method body with a MethodInfo representing I.M(). The name of the method body does not matter.

The code example creates an instance of the emitted class. It obtains a MethodInfo object for I.M(), and uses it to invoke the emitted class's explicit interface implementation. It then obtains a MethodInfo object for A.M(), and uses it to invoke the emitted class's override of that method.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.