Export (0) Print
Expand All

MethodBuilder Class

Defines and represents a method (or constructor) on a dynamic class.

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

[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public sealed class MethodBuilder : MethodInfo

The MethodBuilder type exposes the following members.

  NameDescription
Public propertyAttributesGets the attributes for this method. (Overrides MethodBase.Attributes.)
Public propertyCallingConventionReturns the calling convention of the method. (Overrides MethodBase.CallingConvention.)
Public propertyContainsGenericParametersNot supported for this type. (Overrides MethodBase.ContainsGenericParameters.)
Public propertyDeclaringTypeReturns the type that declares this method. (Overrides MemberInfo.DeclaringType.)
Public propertyInitLocalsGets or sets a Boolean value that specifies whether the local variables in this method are zero-initialized. The default value of this property is true.
Public propertyIsAbstractGets a value that indicates whether the method is abstract (MustInherit in Visual Basic). (Inherited from MethodBase.)
Public propertyIsAssemblyGets a value that indicates whether the potential visibility of this method or constructor is described by MethodAttributes.Assembly; that is, the method or constructor is visible at most to other types in the same assembly, and is not visible to derived types outside the assembly. (Inherited from MethodBase.)
Public propertyIsConstructorGets a value that indicates whether the method is a constructor. (Inherited from MethodBase.)
Public propertyIsFamilyGets a value that indicates whether the visibility of this method or constructor is described by MethodAttributes.Family; that is, the method or constructor is visible only within its class and derived classes. (Inherited from MethodBase.)
Public propertyIsFamilyAndAssemblyGets a value that indicates whether the visibility of this method or constructor is described by MethodAttributes.FamANDAssem; that is, the method or constructor can be called by derived classes, but only if they are in the same assembly. (Inherited from MethodBase.)
Public propertyIsFamilyOrAssemblyGets a value that indicates whether the potential visibility of this method or constructor is described by MethodAttributes.FamORAssem; that is, the method or constructor can be called by derived classes wherever they are, and by classes in the same assembly. (Inherited from MethodBase.)
Public propertyIsFinalGets a value that indicates whether this method is final (NotOverridable in Visual Basic). (Inherited from MethodBase.)
Public propertyIsGenericMethodGets a value that indicates whether the method is a generic method. (Overrides MethodBase.IsGenericMethod.)
Public propertyIsGenericMethodDefinitionGets a value that indicates whether the current MethodBuilder object represents the definition of a generic method. (Overrides MethodBase.IsGenericMethodDefinition.)
Public propertyIsHideBySigGets a value that indicates whether only a member of the same kind with exactly the same signature is hidden in the derived class. (Inherited from MethodBase.)
Public propertyIsPrivateGets a value that indicates whether this member is private. (Inherited from MethodBase.)
Public propertyIsPublicGets a value that indicates whether this is a public method. (Inherited from MethodBase.)
Public propertyIsSpecialNameGets a value that indicates whether this method has a special name. (Inherited from MethodBase.)
Public propertyIsStaticGets a value that indicates whether the method is static (Shared in Visual Basic). (Inherited from MethodBase.)
Public propertyIsVirtualGets a value that indicates whether the method is virtual (Overridable in Visual Basic). (Inherited from MethodBase.)
Public propertyMemberTypeGets a MemberTypes value indicating that this member is a method. (Inherited from MethodInfo.)
Public propertyMetadataTokenGets a value that identifies a metadata element. (Inherited from MemberInfo.)
Public propertyMethodHandleRetrieves the internal handle for the method. Use this handle to access the underlying metadata handle. (Overrides MethodBase.MethodHandle.)
Public propertyModuleGets the module in which the current method is being defined. (Overrides MemberInfo.Module.)
Public propertyNameRetrieves the name of this method. (Overrides MemberInfo.Name.)
Public propertyReflectedTypeRetrieves the class that was used in reflection to obtain this object. (Overrides MemberInfo.ReflectedType.)
Public propertyReturnParameterGets a ParameterInfo object that contains information about the return type of the method, such as whether the return type has custom modifiers. (Overrides MethodInfo.ReturnParameter.)
Public propertyReturnTypeGets the return type of the method represented by this MethodBuilder. (Overrides MethodInfo.ReturnType.)
Public propertyReturnTypeCustomAttributesReturns the custom attributes of the method's return type. (Overrides MethodInfo.ReturnTypeCustomAttributes.)
Public propertySignatureRetrieves the signature of the method.
Top

  NameDescription
Public methodCreateDelegate(Type)Creates a delegate of the specified type from this method. (Inherited from MethodInfo.)
Public methodCreateDelegate(Type, Object)Creates a delegate of the specified type with the specified target from this method. (Inherited from MethodInfo.)
Public methodDefineGenericParametersSets the number of generic type parameters for the current method, specifies their names, and returns an array of GenericTypeParameterBuilder objects that can be used to define their constraints.
Public methodDefineParameterSets the parameter attributes and the name of a parameter of this method, or of the return value of this method. Returns a ParameterBuilder that can be used to apply custom attributes.
Public methodEqualsDetermines whether the given object is equal to this instance. (Overrides Object.Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetBaseDefinitionReturns the base implementation for a method. (Overrides MethodInfo.GetBaseDefinition().)
Public methodGetCustomAttributes(Boolean)Returns all the custom attributes defined for this method. (Overrides MemberInfo.GetCustomAttributes(Boolean).)
Public methodGetCustomAttributes(Type, Boolean)Returns the custom attributes identified by the given type. (Overrides MemberInfo.GetCustomAttributes(Type, Boolean).)
Public methodGetGenericArgumentsReturns an array of GenericTypeParameterBuilder objects that represent the type parameters of the method, if it is generic. (Overrides MethodInfo.GetGenericArguments().)
Public methodGetGenericMethodDefinitionReturns this method. (Overrides MethodInfo.GetGenericMethodDefinition().)
Public methodGetHashCodeGets the hash code for this method. (Overrides Object.GetHashCode().)
Public methodGetILGenerator()Returns an ILGenerator for this method with a default Microsoft intermediate language (MSIL) stream size of 64 bytes.
Public methodGetILGenerator(Int32)Returns an ILGenerator for this method with the specified Microsoft intermediate language (MSIL) stream size.
Public methodGetMethodImplementationFlagsReturns the implementation flags for the method. (Overrides MethodBase.GetMethodImplementationFlags().)
Public methodGetModuleReturns a reference to the module that contains this method.
Public methodGetParametersReturns the parameters of this method. (Overrides MethodBase.GetParameters().)
Public methodGetTokenReturns the MethodToken that represents the token for this method.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInvoke(Object, Object[])Invokes the method or constructor represented by the current instance, using the specified parameters. (Inherited from MethodBase.)
Public methodInvoke(Object, BindingFlags, Binder, Object[], CultureInfo)Dynamically invokes the method reflected by this instance on the given object, passing along the specified parameters, and under the constraints of the given binder. (Overrides MethodBase.Invoke(Object, BindingFlags, Binder, Object[], CultureInfo).)
Public methodIsDefinedChecks if the specified custom attribute type is defined. (Overrides MemberInfo.IsDefined(Type, Boolean).)
Public methodMakeGenericMethodReturns a generic method constructed from the current generic method definition using the specified generic type arguments. (Overrides MethodInfo.MakeGenericMethod(Type[]).)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSetCustomAttributeSets a custom attribute using a custom attribute builder.
Public methodSetImplementationFlagsSets the implementation flags for this method.
Public methodSetParametersSets the number and types of parameters for a method.
Public methodSetReturnTypeSets the return type of the method.
Public methodSetSignatureSets the method signature, including the return type, the parameter types, and the required and optional custom modifiers of the return type and parameter types.
Public methodToStringReturns this MethodBuilder instance as a string. (Overrides Object.ToString().)
Top

MethodBuilder is used to fully describe a method in Microsoft intermediate language (MSIL), including the name, attributes, signature, and method body. It is used in conjunction with the TypeBuilder class to create classes at runtime.

The following example shows how to use the MethodBuilder class to create a method within a dynamic type. The example defines a dynamic assembly with one module. The module in the example assembly contains one type, MyDynamicType, which has a private field, a property that gets and sets the private field, constructors that initialize the private field, and a method that multiplies a user-supplied number by the private field value and returns the result.


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

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // In this version of the runtime, an assembly consists of one 
      // module which contains zero or more types. This example 
      // creates an assembly containing one public type named
      // "MyDynamicType". The type has a private field, a property 
      // that gets and sets the private field, constructors that 
      // initialize the private field, and a method that multiplies 
      // a user-supplied number by the private field value and returns
      // the result. In C# the type might look like this:
      /*
      public class MyDynamicType
      {
          private int m_number;

          public MyDynamicType() : this(42) {}
          public MyDynamicType(int initNumber)
          {
              m_number = initNumber;
          }

          public int Number
          {
              get { return m_number; }
              set { m_number = value; }
          }

          public int MyMethod(int multiplier)
          {
              return m_number * multiplier;
          }
      }
      */

      AssemblyName aName = new AssemblyName("DynamicAssemblyExample");
      AssemblyBuilder ab =
          AppDomain.CurrentDomain.DefineDynamicAssembly(
              aName,
              AssemblyBuilderAccess.Run);

      // Create the module.
      ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);

      TypeBuilder tb = mb.DefineType(
          "MyDynamicType",
           TypeAttributes.Public);

      // Add a private field of type int (Int32).
      FieldBuilder fbNumber = tb.DefineField(
          "m_number",
          typeof(int),
          FieldAttributes.Private);

      // Define a constructor that takes an integer argument and 
      // stores it in the private field. 
      Type[] parameterTypes = { typeof(int) };
      ConstructorBuilder ctor1 = tb.DefineConstructor(
          MethodAttributes.Public,
          CallingConventions.Standard,
          parameterTypes);

      ILGenerator ctor1IL = ctor1.GetILGenerator();
      // For a constructor, argument zero is a reference to the new
      // instance. Push it on the stack before calling the base
      // class constructor. Specify the default constructor of the 
      // base class (System.Object) by passing an empty array of 
      // types (Type.EmptyTypes) to GetConstructor.
      ctor1IL.Emit(OpCodes.Ldarg_0);
      ctor1IL.Emit(OpCodes.Call,
          typeof(object).GetConstructor(Type.EmptyTypes));
      // Push the instance on the stack before pushing the argument
      // that is to be assigned to the private field m_number.
      ctor1IL.Emit(OpCodes.Ldarg_0);
      ctor1IL.Emit(OpCodes.Ldarg_1);
      ctor1IL.Emit(OpCodes.Stfld, fbNumber);
      ctor1IL.Emit(OpCodes.Ret);

      // Define a default constructor that supplies a default value
      // for the private field. For parameter types, pass the empty
      // array of types or pass null.
      ConstructorBuilder ctor0 = tb.DefineConstructor(
          MethodAttributes.Public,
          CallingConventions.Standard,
          Type.EmptyTypes);

      ILGenerator ctor0IL = ctor0.GetILGenerator();
      // For a constructor, argument zero is a reference to the new
      // instance. Push it on the stack before pushing the default
      // value on the stack, then call constructor ctor1.
      ctor0IL.Emit(OpCodes.Ldarg_0);
      ctor0IL.Emit(OpCodes.Ldc_I4_S, 42);
      ctor0IL.Emit(OpCodes.Call, ctor1);
      ctor0IL.Emit(OpCodes.Ret);

      // Define a property named Number that gets and sets the private 
      // field.
      //
      // The last argument of DefineProperty is null, because the
      // property has no parameters. (If you don't specify null, you must
      // specify an array of Type objects. For a parameterless property,
      // use the built-in array with no elements: Type.EmptyTypes)
      PropertyBuilder pbNumber = tb.DefineProperty(
          "Number",
          PropertyAttributes.HasDefault,
          typeof(int),
          null);

      // The property "set" and property "get" methods require a special
      // set of attributes.
      MethodAttributes getSetAttr = MethodAttributes.Public |
          MethodAttributes.SpecialName | MethodAttributes.HideBySig;

      // Define the "get" accessor method for Number. The method returns
      // an integer and has no arguments. (Note that null could be 
      // used instead of Types.EmptyTypes)
      MethodBuilder mbNumberGetAccessor = tb.DefineMethod(
          "get_Number",
          getSetAttr,
          typeof(int),
          Type.EmptyTypes);

      ILGenerator numberGetIL = mbNumberGetAccessor.GetILGenerator();
      // For an instance property, argument zero is the instance. Load the 
      // instance, then load the private field and return, leaving the
      // field value on the stack.
      numberGetIL.Emit(OpCodes.Ldarg_0);
      numberGetIL.Emit(OpCodes.Ldfld, fbNumber);
      numberGetIL.Emit(OpCodes.Ret);

      // Define the "set" accessor method for Number, which has no return
      // type and takes one argument of type int (Int32).
      MethodBuilder mbNumberSetAccessor = tb.DefineMethod(
          "set_Number",
          getSetAttr,
          null,
          new Type[] { typeof(int) });

      ILGenerator numberSetIL = mbNumberSetAccessor.GetILGenerator();
      // Load the instance and then the numeric argument, then store the
      // argument in the field.
      numberSetIL.Emit(OpCodes.Ldarg_0);
      numberSetIL.Emit(OpCodes.Ldarg_1);
      numberSetIL.Emit(OpCodes.Stfld, fbNumber);
      numberSetIL.Emit(OpCodes.Ret);

      // Last, map the "get" and "set" accessor methods to the 
      // PropertyBuilder. The property is now complete. 
      pbNumber.SetGetMethod(mbNumberGetAccessor);
      pbNumber.SetSetMethod(mbNumberSetAccessor);

      // Define a method that accepts an integer argument and returns
      // the product of that integer and the private field m_number. This
      // time, the array of parameter types is created on the fly.
      MethodBuilder meth = tb.DefineMethod(
          "MyMethod",
          MethodAttributes.Public,
          typeof(int),
          new Type[] { typeof(int) });

      ILGenerator methIL = meth.GetILGenerator();
      // To retrieve the private instance field, load the instance it
      // belongs to (argument zero). After loading the field, load the 
      // argument one and then multiply. Return from the method with 
      // the return value (the product of the two numbers) on the 
      // execution stack.
      methIL.Emit(OpCodes.Ldarg_0);
      methIL.Emit(OpCodes.Ldfld, fbNumber);
      methIL.Emit(OpCodes.Ldarg_1);
      methIL.Emit(OpCodes.Mul);
      methIL.Emit(OpCodes.Ret);

      // Finish the type.
      Type t = tb.CreateType();

      // The code can be executed immediately. Start by getting reflection
      // objects for the method and the property.
      MethodInfo mi = t.GetMethod("MyMethod");
      PropertyInfo pi = t.GetProperty("Number");

      // Create an instance of MyDynamicType using the default 
      // constructor. 
      object o1 = Activator.CreateInstance(t);

      // Display the value of the property, then change it to 127 and 
      // display it again. Use null to indicate that the property
      // has no index.
      outputBlock.Text += String.Format("o1.Number: {0}\n", pi.GetValue(o1, null));
      pi.SetValue(o1, 127, null);
      outputBlock.Text += String.Format("o1.Number: {0}\n", pi.GetValue(o1, null));

      // Call MyMethod, passing 22, and display the return value, 22
      // times 127. Arguments must be passed as an array, even when
      // there is only one.
      object[] arguments = { 22 };
      outputBlock.Text += String.Format("o1.MyMethod(22): {0}\n",
          mi.Invoke(o1, arguments));

      // Create an instance of MyDynamicType using the constructor
      // that specifies m_Number. The constructor is identified by
      // matching the types in the argument array. In this case, 
      // the argument array is created on the fly. Display the 
      // property value.
      object o2 = Activator.CreateInstance(t,
          new object[] { 5280 });
      outputBlock.Text += String.Format("o2.Number: {0}\n", pi.GetValue(o2, null));
   }
}

/* This code produces the following output:

o1.Number: 42
o1.Number: 127
o1.MyMethod(22): 2794
o2.Number: 5280
 */


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft