TypeBuilder.DefineMethod Method (String, MethodAttributes, CallingConventions, Type, Type())

Adds a new method to the type, with the specified name, method attributes, calling convention, and method signature.

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

'Declaration
Public Function DefineMethod ( _
	name As String, _
	attributes As MethodAttributes, _
	callingConvention As CallingConventions, _
	returnType As Type, _
	parameterTypes As Type() _
) As MethodBuilder

Parameters

name
Type: System.String

The name of the method. name cannot contain embedded nulls.

attributes
Type: System.Reflection.MethodAttributes

The attributes of the method.

callingConvention
Type: System.Reflection.CallingConventions

The calling convention of the method.

returnType
Type: System.Type

The return type of the method.

parameterTypes
Type: System.Type()

The types of the parameters of the method.

Return Value

Type: System.Reflection.Emit.MethodBuilder
A MethodBuilder representing the newly defined method.

ExceptionCondition
ArgumentException

The length of name is zero.

-or-

The type of the parent of this method is an interface, and this method is not virtual (Overridable in Visual Basic).

ArgumentNullException

name is Nothing.

InvalidOperationException

The type was previously created using CreateType.

-or-

For the current dynamic type, the IsGenericType property is true, but the IsGenericTypeDefinition property is false.

The following code sample demonstrates the use of DefineMethod to set a constructor's particular signature and attributes on a dynamic type and to return a corresponding MethodBuilder for MSIL population.

Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions

Public Interface IMyInterface
   Function HelloMethod(parameter As String) As String 
End Interface 'IMyInterface

Public Class Example
   <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
   Public Shared Sub Main()
      Dim myNestedClassType As Type = CreateCallee(Thread.GetDomain())
      ' Create an instance of 'MyNestedClass'. 
      Dim myInterface As IMyInterface = _
            CType(Activator.CreateInstance(myNestedClassType), IMyInterface)
      Console.WriteLine(myInterface.HelloMethod("Bill"))
   End Sub 'Main

   ' Create the callee transient dynamic assembly. 
   Private Shared Function CreateCallee(myAppDomain As AppDomain) As Type
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "Example" 
      ' Create the callee dynamic assembly. 
      Dim myAssembly As AssemblyBuilder = _
               myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)
      ' Create a dynamic module in the callee assembly. 
      Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
      ' Define a public class named "MyHelloWorld". 
      Dim myHelloWorldType As TypeBuilder = _
               myModule.DefineType("MyHelloWorld", TypeAttributes.Public)
      ' Define a public nested class named 'MyNestedClass'. 
      Dim myNestedClassType As TypeBuilder = _
               myHelloWorldType.DefineNestedType("MyNestedClass", TypeAttributes.NestedPublic, _
               GetType(Example), New Type() {GetType(IMyInterface)})
      ' Implement 'IMyInterface' interface.
      myNestedClassType.AddInterfaceImplementation(GetType(IMyInterface))
      ' Define 'HelloMethod' of 'IMyInterface'. 
      Dim myHelloMethod As MethodBuilder = _
               myNestedClassType.DefineMethod("HelloMethod", MethodAttributes.Public Or _
               MethodAttributes.Virtual, GetType(String), New Type() {GetType(String)})
      ' Generate IL for 'GetGreeting' method. 
      Dim myMethodIL As ILGenerator = myHelloMethod.GetILGenerator()
      myMethodIL.Emit(OpCodes.Ldstr, "Hi! ")
      myMethodIL.Emit(OpCodes.Ldarg_1)
      Dim infoMethod As MethodInfo = _
               GetType(String).GetMethod("Concat", New Type() {GetType(String), GetType(String)})
      myMethodIL.Emit(OpCodes.Call, infoMethod)
      myMethodIL.Emit(OpCodes.Ret)

      Dim myHelloMethodInfo As MethodInfo = GetType(IMyInterface).GetMethod("HelloMethod")
      ' Implement 'HelloMethod' of 'IMyInterface'.
      myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo)
      ' Create 'MyHelloWorld' type. 
      Dim myType As Type = myHelloWorldType.CreateType()
      ' Create 'MyNestedClass' type. 
      Return myNestedClassType.CreateType()
   End Function 'CreateCallee
End Class 'Example

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft