MethodBuilder::DeclaringType Property

 

Returns the type that declares this method.

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

public:
property Type^ DeclaringType {
	virtual Type^ get() override;
}

Property Value

Type: System::Type^

Read-only. The type that declares this method.

The following code illustrates the use of the Type property.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
int main()
{
   try
   {
      // Get the current AppDomain.
      AppDomain^ myAppDomain = AppDomain::CurrentDomain;
      AssemblyName^ myAssemblyName = gcnew AssemblyName;
      myAssemblyName->Name = "MyDynamicAssembly";

      // Create the dynamic assembly and set its access mode to 'Save'.
      AssemblyBuilder^ myAssemblyBuilder = myAppDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::Save );

      // Create a dynamic module 'myModuleBuilder'.
      ModuleBuilder^ myModuleBuilder = myAssemblyBuilder->DefineDynamicModule( "MyDynamicModule", true );

      // Define a public class 'MyDynamicClass'.
      TypeBuilder^ myTypeBuilder = myModuleBuilder->DefineType( "MyDynamicClass", TypeAttributes::Public );

      // Define a public string field named 'myField'.
      FieldBuilder^ myField = myTypeBuilder->DefineField( "MyDynamicField", String::typeid, FieldAttributes::Public );

      // Define the dynamic method 'MyDynamicMethod'.
      array<Type^>^temp0 = gcnew array<Type^>(0);
      MethodBuilder^ myMethodBuilder = myTypeBuilder->DefineMethod( "MyDynamicMethod", MethodAttributes::Private, int::typeid, temp0 );

      // Generate the IL for 'myMethodBuilder'.
      ILGenerator^ myMethodIL = myMethodBuilder->GetILGenerator();

      // Emit the necessary opcodes.
      myMethodIL->Emit( OpCodes::Ldarg_0 );
      myMethodIL->Emit( OpCodes::Ldfld, myField );
      myMethodIL->Emit( OpCodes::Ret );

      // Create 'myTypeBuilder' class.
      Type^ myType1 = myTypeBuilder->CreateType();

      // Get the method information of 'myTypeBuilder'.
      array<MethodInfo^>^myInfo = myType1->GetMethods( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) );

      // Print non-public methods present of 'myType1'.
      Console::WriteLine( "\nThe Non-Public methods present in 'myType1' are:\n" );
      for ( int i = 0; i < myInfo->Length; i++ )
      {
         Console::WriteLine( myInfo[ i ]->Name );
      }
      Console::WriteLine( "\nThe Attribute of 'MyDynamicMethod' is :{0}", myMethodBuilder->Attributes );
      Console::WriteLine( "\nThe Signature of 'MyDynamicMethod' is : \n{0}", myMethodBuilder->Signature );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception :{0}", e->Message );
   }
}

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