TypeBuilder::DefineMethod Method (String^, MethodAttributes, CallingConventions, Type^, array<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)

public:
MethodBuilder^ DefineMethod(
	String^ name,
	MethodAttributes attributes,
	CallingConventions callingConvention,
	Type^ returnType,
	array<Type^>^ parameterTypes
)

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: array<System::Type^>^

The types of the parameters of the method.

Return Value

Type: System.Reflection.Emit::MethodBuilder^

A MethodBuilder representing the newly defined method.

Exception Condition
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 null.

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.

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
public interface class IMyInterface
{
   String^ HelloMethod( String^ parameter );
};

public ref class EmittedClass
{
public:
   // Because this method calls Activator::CreateInstance, 
   // it requires full trust. 
   [System::Security::Permissions::PermissionSetAttribute
	(System::Security::Permissions::SecurityAction::Demand, Name = "FullTrust")]
   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( "Bill" ) );
   }

private:

   // Create the callee transient dynamic assembly.
   static Type^ CreateCallee( AppDomain^ myAppDomain )
   {
      AssemblyName^ myAssemblyName = gcnew 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'.
      array<Type^>^temp0 = {IMyInterface::typeid};
      TypeBuilder^ myNestedClassType = myHelloWorldType->DefineNestedType( "MyNestedClass", TypeAttributes::NestedPublic, EmittedClass::typeid, temp0 );

      // Implement 'IMyInterface' interface.
      myNestedClassType->AddInterfaceImplementation( IMyInterface::typeid );

      // Define 'HelloMethod' of 'IMyInterface'.
      array<Type^>^temp1 = {String::typeid};
      MethodBuilder^ myHelloMethod = myNestedClassType->DefineMethod( "HelloMethod", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Virtual), String::typeid, temp1 );

      // Generate IL for 'GetGreeting' method.
      ILGenerator^ myMethodIL = myHelloMethod->GetILGenerator();
      myMethodIL->Emit( OpCodes::Ldstr, "Hi! " );
      myMethodIL->Emit( OpCodes::Ldarg_1 );
      array<Type^>^temp2 = {String::typeid,String::typeid};
      MethodInfo^ infoMethod = String::typeid->GetMethod( "Concat", temp2 );
      myMethodIL->Emit( OpCodes::Call, infoMethod );
      myMethodIL->Emit( OpCodes::Ret );
      MethodInfo^ myHelloMethodInfo = IMyInterface::typeid->GetMethod( "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();
}

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Return to top
Show: