MethodBuilder.DeclaringType Property

Definition

Returns the type that declares this method.

C#
public override Type? DeclaringType { get; }
C#
public override Type DeclaringType { get; }

Property Value

Read-only. The type that declares this method.

Examples

The following code illustrates the use of the Type property.

C#
using System;
using System.Reflection;
using System.Reflection.Emit;

public class MethodBuilderClass
{
   public static void Main()
   {
      try
      {
         // 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",
                                                 TypeAttributes.Public);
         // 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.Ldarg_0);
         myMethodIL.Emit(OpCodes.Ldfld, myField);
         myMethodIL.Emit(OpCodes.Ret);

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

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

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1