Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

MethodBuilder.DeclaringType Property

Returns the type that declares this method.

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

public override Type DeclaringType { get; }
/** @property */
public Type get_DeclaringType ()

public override function get DeclaringType () : Type

Property Value

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()
   {
      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);
      }
   }
}

import System.*;
import System.Reflection.*;
import System.Reflection.Emit.*;

public class MethodBuilderClass
{
    public static void main(String[] args)
    {
        try {
            // Get the current AppDomain.
            AppDomain myAppDomain = AppDomain.get_CurrentDomain();
            AssemblyName myAssemblyName = new AssemblyName();
            myAssemblyName.set_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.class.ToType(), FieldAttributes.Public);

            // Define the dynamic method 'MyDynamicMethod'.
            MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod(
                "MyDynamicMethod", MethodAttributes.Private, int.class.ToType(),
                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(((MethodInfo)myInfo.get_Item(i)).get_Name());
            }

            // Print the 'Attribute', 'Signature' of 'myMethodBuilder'.
            Console.WriteLine("\nThe Attribute of 'MyDynamicMethod' is :{0}",
                myMethodBuilder.get_Attributes());
            Console.WriteLine("\nThe Signature of 'MyDynamicMethod' is : \n" 
                + myMethodBuilder.get_Signature());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception :{0}", e.get_Message());
        }
    } //main
} //MethodBuilderClass

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.