This documentation is archived and is not being maintained.

MethodBuilder.DeclaringType Property

Returns the type that declares this method.

[Visual Basic]
Overrides Public ReadOnly Property DeclaringType As Type
public override Type DeclaringType {get;}
public: __property Type* get_DeclaringType();
public override function get DeclaringType() : Type;

Property Value

Read-only. The type that declares this method.


[Visual Basic, C#, C++] The following code illustrates the use of the Type property.

[Visual Basic] 
Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports MicroSoft.VisualBasic

Public Class MethodBuilderClass

   Public Shared Sub Main()
         ' Get the current AppDomain.
         Dim myAppDomain As AppDomain = AppDomain.CurrentDomain
         Dim myAssemblyName As New AssemblyName()
         myAssemblyName.Name = "MyDynamicAssembly"

         ' Create the dynamic assembly and set its access mode to 'Save'.
         Dim myAssemblyBuilder As AssemblyBuilder = myAppDomain.DefineDynamicAssembly _
                                                      (myAssemblyName, AssemblyBuilderAccess.Save)
         ' Create a dynamic module 'myModuleBuilder'.
         Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule _
                                                                     ("MyDynamicModule", True)
         ' Define a public class 'MyDynamicClass'.
         Dim myTypeBuilder As TypeBuilder = myModuleBuilder.DefineType _
                                                      ("MyDynamicClass", TypeAttributes.Public)
         ' Define a public string field named 'myField'.
         Dim myField As FieldBuilder = myTypeBuilder.DefineField("MyDynamicField", _
                                                   GetType(String), FieldAttributes.Public)
         ' Define the dynamic method 'MyDynamicMethod'.
         Dim myMethodBuilder As MethodBuilder = myTypeBuilder.DefineMethod("MyDynamicMethod", _
                                 MethodAttributes.Private, GetType(Integer), New Type() {})
         ' Generate the IL for 'myMethodBuilder'.
         Dim myMethodIL As ILGenerator = myMethodBuilder.GetILGenerator()
         ' Emit the necessary opcodes.
         myMethodIL.Emit(OpCodes.Ldfld, myField)

         ' Create 'myTypeBuilder' class.
         Dim myType1 As Type = myTypeBuilder.CreateType()

         ' Get the method information of 'myTypeBuilder'.
         Dim myInfo As MethodInfo() = myType1.GetMethods(BindingFlags.NonPublic Or _
         ' Print non-public methods present of 'myType1'.
         Console.WriteLine(ControlChars.Newline + "The Non-Public methods present in 'myType1' are:" + _
         Dim i As Integer
         For i = 0 To myInfo.Length - 1
         Next i
         ' Print the 'Attribute', 'Signature' of 'myMethodBuilder'.
         Console.WriteLine(ControlChars.Newline + "The Attribute of 'MyDynamicMethod' is :{0}", _
         Console.WriteLine(ControlChars.Newline + "The Signature of 'MyDynamicMethod' is : " + _
                                                ControlChars.Newline + myMethodBuilder.Signature)
      Catch e As Exception
         Console.WriteLine("Exception :{0}", e.Message)
      End Try
   End Sub 'Main
End Class 'MethodBuilderClass

using System;
using System.Reflection;
using System.Reflection.Emit;

public class MethodBuilderClass
   public static void Main()
         // Get the current AppDomain.
         AppDomain myAppDomain = AppDomain.CurrentDomain;
         AssemblyName myAssemblyName = new 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",
         // Define a public string field named 'myField'.
         FieldBuilder myField = myTypeBuilder.DefineField("MyDynamicField",
                        typeof(String), FieldAttributes.Public);
         // Define the dynamic method 'MyDynamicMethod'.
         MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("MyDynamicMethod",
                              MethodAttributes.Private, typeof(int), new Type[] {});
         // Generate the IL for 'myMethodBuilder'.
         ILGenerator myMethodIL = myMethodBuilder.GetILGenerator();
         // Emit the necessary opcodes.
         myMethodIL.Emit(OpCodes.Ldfld, myField);

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

         // Get the method information of 'myTypeBuilder'.
         MethodInfo[] myInfo = myType1.GetMethods(BindingFlags.NonPublic |
         // 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++)
         // Print the 'Attribute', 'Signature' of 'myMethodBuilder'.
         Console.WriteLine("\nThe Attribute of 'MyDynamicMethod' is :{0}" ,
         Console.WriteLine("\nThe Signature of 'MyDynamicMethod' is : \n"
                                    + myMethodBuilder.Signature);
      catch(Exception e)
         Console.WriteLine("Exception :{0}", e.Message);

#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

int main()
      // Get the current AppDomain.
      AppDomain* myAppDomain = AppDomain::CurrentDomain;
      AssemblyName* myAssemblyName = new AssemblyName();
      myAssemblyName->Name = S"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(S"MyDynamicModule", true);
      // Define a public class 'MyDynamicClass'.
      TypeBuilder* myTypeBuilder = myModuleBuilder->DefineType(S"MyDynamicClass",
      // Define a public string field named 'myField'.
      FieldBuilder* myField = myTypeBuilder->DefineField(S"MyDynamicField",
         __typeof(String), FieldAttributes::Public);

      // Define the dynamic method 'MyDynamicMethod'.
      Type* temp0 [] = new Type* [0];
      MethodBuilder* myMethodBuilder = myTypeBuilder->DefineMethod(S"MyDynamicMethod",
         MethodAttributes::Private, __typeof(int), temp0);
      // Generate the IL for 'myMethodBuilder'.
      ILGenerator* myMethodIL = myMethodBuilder->GetILGenerator();
      // Emit the necessary opcodes.
      myMethodIL->Emit(OpCodes::Ldfld, myField);

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

      // Get the method information of 'myTypeBuilder'.
      MethodInfo* myInfo[] = myType1->GetMethods(
         static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance));
      // Print non-public methods present of 'myType1'.
      Console::WriteLine(S"\nThe Non-Public methods present in 'myType1' are:\n");
      for(int i = 0; i < myInfo->Length; i++)
      // Print the 'Attribute', 'Signature' of 'myMethodBuilder'.
      Console::WriteLine(S"\nThe Attribute of 'MyDynamicMethod' is :{0}",
      Console::WriteLine(S"\nThe Signature of 'MyDynamicMethod' is : \n{0}",
   catch(Exception* e)
      Console::WriteLine(S"Exception :{0}", e->Message);

[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.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

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