Export (0) Print
Expand All

TypeBuilder.DefineMethod Method

Defines a method.

Overload List

Adds a new method to the class, with the given name and method signature.

[Visual Basic] Overloads Public Function DefineMethod(String, MethodAttributes, Type, Type()) As MethodBuilder
[C#] public MethodBuilder DefineMethod(string, MethodAttributes, Type, Type[]);
[C++] public: MethodBuilder* DefineMethod(String*, MethodAttributes, Type*, Type[]);
[JScript] public function DefineMethod(String, MethodAttributes, Type, Type[]) : MethodBuilder;

Adds a new method to the class, with the given name and method signature.

[Visual Basic] Overloads Public Function DefineMethod(String, MethodAttributes, CallingConventions, Type, Type()) As MethodBuilder
[C#] public MethodBuilder DefineMethod(string, MethodAttributes, CallingConventions, Type, Type[]);
[C++] public: MethodBuilder* DefineMethod(String*, MethodAttributes, CallingConventions, Type*, Type[]);
[JScript] public function DefineMethod(String, MethodAttributes, CallingConventions, Type, Type[]) : MethodBuilder;

Example

[Visual Basic, C#, C++] The following code sample demonstrates the use of DefineMethod to set a constructor's particular signature and attributes on a dynamic type and return a corresponding MethodBuilder for IL population.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of DefineMethod. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit


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

Public Class EmittedClass
   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 = "EmittedClass"
      ' 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(EmittedClass), 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 'EmittedClass

[C#] 
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;


public interface IMyInterface
{
   String HelloMethod(String parameter);
}

public class EmittedClass
{
   public static void Main()
   {
      Type myNestedClassType = CreateCallee(Thread.GetDomain());
      // Cretae an instance of 'MyNestedClass'.
      IMyInterface myInterface =
         (IMyInterface)Activator.CreateInstance(myNestedClassType);
      Console.WriteLine(myInterface.HelloMethod("Bill"));
   }

   // Create the callee transient dynamic assembly.
   private static Type CreateCallee(AppDomain myAppDomain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedClass";
      // Create the callee dynamic assembly.
      AssemblyBuilder myAssembly =
         myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
      // Create a dynamic module in the callee assembly.
      ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
      // Define a public class named "MyHelloWorld".
      TypeBuilder myHelloWorldType =
         myModule.DefineType("MyHelloWorld", TypeAttributes.Public);
      // Define a public nested class named 'MyNestedClass'.
      TypeBuilder myNestedClassType =
         myHelloWorldType.DefineNestedType("MyNestedClass",
            TypeAttributes.NestedPublic, typeof(EmittedClass),
            new Type[]{typeof(IMyInterface)});
      // Implement 'IMyInterface' interface.
      myNestedClassType.AddInterfaceImplementation(typeof(IMyInterface));
      // Define 'HelloMethod' of 'IMyInterface'.
      MethodBuilder myHelloMethod =
         myNestedClassType.DefineMethod("HelloMethod",
            MethodAttributes.Public | MethodAttributes.Virtual,
            typeof(String), new Type[]{typeof(String)});
      // Generate IL for 'GetGreeting' method.
      ILGenerator myMethodIL = myHelloMethod.GetILGenerator();
      myMethodIL.Emit(OpCodes.Ldstr, "Hi! ");
      myMethodIL.Emit(OpCodes.Ldarg_1);
      MethodInfo infoMethod =
         typeof(String).GetMethod("Concat",new Type[]{typeof(string),typeof(string)});
      myMethodIL.Emit(OpCodes.Call, infoMethod);
      myMethodIL.Emit(OpCodes.Ret);

      MethodInfo myHelloMethodInfo =
         typeof(IMyInterface).GetMethod("HelloMethod");
      // Implement 'HelloMethod' of 'IMyInterface'.
      myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo);
      // Create 'MyHelloWorld' type.
      Type myType = myHelloWorldType.CreateType();
      // Create 'MyNestedClass' type.
      return myNestedClassType.CreateType();
   }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;


public __gc __interface IMyInterface
{
    String* HelloMethod(String* parameter);
};

public __gc class EmittedClass
{
public:
    static void Main()
    {
        Type* myNestedClassType = CreateCallee(Thread::GetDomain());
        // Create an instance of 'MyNestedClass'.
        IMyInterface* myInterface =
            dynamic_cast<IMyInterface*>(Activator::CreateInstance(myNestedClassType));
        Console::WriteLine(myInterface->HelloMethod(S"Bill"));
    }

    // Create the callee transient dynamic assembly.
private:
    static Type* CreateCallee(AppDomain* myAppDomain)
    {
        AssemblyName* myAssemblyName = new AssemblyName();
        myAssemblyName->Name = S"EmittedClass";
        // Create the callee dynamic assembly.
        AssemblyBuilder* myAssembly =
            myAppDomain->DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess::Run);
        // Create a dynamic module in the callee assembly.
        ModuleBuilder* myModule = myAssembly->DefineDynamicModule(S"EmittedModule");
        // Define a public class named "MyHelloWorld".
        TypeBuilder* myHelloWorldType =
            myModule->DefineType(S"MyHelloWorld", TypeAttributes::Public);
        // Define a public nested class named 'MyNestedClass'.
        Type* temp0 [] = {__typeof(IMyInterface)};
        TypeBuilder* myNestedClassType =
            myHelloWorldType->DefineNestedType(S"MyNestedClass",
            TypeAttributes::NestedPublic, __typeof(EmittedClass),
            temp0);
        // Implement 'IMyInterface' interface.
        myNestedClassType->AddInterfaceImplementation(__typeof(IMyInterface));
        // Define 'HelloMethod' of 'IMyInterface'.
        Type* temp1 [] = {__typeof(String)};
        MethodBuilder* myHelloMethod =
            myNestedClassType->DefineMethod(S"HelloMethod",
                static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Virtual),
            __typeof(String), temp1);
        // Generate IL for 'GetGreeting' method.
        ILGenerator* myMethodIL = myHelloMethod->GetILGenerator();
        myMethodIL->Emit(OpCodes::Ldstr, S"Hi! ");
        myMethodIL->Emit(OpCodes::Ldarg_1);
        Type* temp2 [] = {__typeof(String),__typeof(String)};
        MethodInfo* infoMethod =
            __typeof(String)->GetMethod(S"Concat",temp2);
        myMethodIL->Emit(OpCodes::Call, infoMethod);
        myMethodIL->Emit(OpCodes::Ret);

        MethodInfo* myHelloMethodInfo =
            __typeof(IMyInterface)->GetMethod(S"HelloMethod");
        // Implement 'HelloMethod' of 'IMyInterface'.
        myNestedClassType->DefineMethodOverride(myHelloMethod, myHelloMethodInfo);
        // Create 'MyHelloWorld' type.
        Type* myType = myHelloWorldType->CreateType();
        // Create 'MyNestedClass' type.
        return myNestedClassType->CreateType();
    }
};

int main()
{
    EmittedClass::Main();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

TypeBuilder Class | TypeBuilder Members | System.Reflection.Emit Namespace

Show:
© 2014 Microsoft