MethodBuilder.DeclaringType Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Returns the type that declares this method.

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

public override Type DeclaringType { get; }

Property Value

Type: System.Type

Read-only. The type that declares this method.

The following code illustrates the use of the Type property.

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);

.NET Framework
Available since 1.1
Available since 2.0
Return to top