System.Reflection.Emit.MethodBuilder Class

Assembly: Mscorlib.dll
Namespace: System.Reflection.Emit
Summary
Defines and represents a method (or constructor) on a dynamic class.
C# Syntax:
public sealed class MethodBuilder : MethodInfo
Thread Safety
Reflection Emit is thread-safe when using assemblies that were created with the AppDomain.DefineDynamicAssembly method with the Boolean parameter isSynchronized set to true.
Remarks
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.
See also:
System.Reflection.Emit Namespace

System.Reflection.Emit.MethodBuilder Member List:

Public Properties
Attributes Read-only

Overridden:
Retrieves the attributes for this method.
CallingConvention Read-only

Overridden:
Returns the calling convention of the method.
DeclaringType Read-only

Overridden:
Returns the type that declares this method.
InitLocals Read-write

Gets or sets whether the local variables in this method should be zero initialized. The default value of this property is true.
IsAbstract
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsAbstract


Gets a value indicating whether the method is abstract.
IsAssembly
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsAssembly


Gets a value indicating whether this method can be called by other classes in the same assembly.
IsConstructor
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsConstructor


Gets a value indicating whether the method is a constructor.
IsFamily
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsFamily


Gets a value indicating whether access to this method is restricted to members of the class and members of its derived classes.
IsFamilyAndAssembly
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsFamilyAndAssembly


Gets a value indicating whether this method can be called by derived classes if they are in the same assembly.
IsFamilyOrAssembly
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsFamilyOrAssembly


Gets a value indicating whether this method can be called by derived classes, wherever they are, and by all classes in the same assembly.
IsFinal
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsFinal


Gets a value indicating whether this method is final.
IsHideBySig
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsHideBySig


Gets a value indicating whether only a member of the same kind with exactly the same signature is hidden in the derived class.
IsPrivate
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsPrivate


Gets a value indicating whether this member is private.
IsPublic
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsPublic


Gets a value indicating whether this is a public method.
IsSpecialName
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsSpecialName


Gets a value indicating whether this method has a special name.
IsStatic
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsStatic


Gets a value indicating whether the method is static.
IsVirtual
(inherited from System.Reflection.MethodBase)
Read-only

See base class member description: System.Reflection.MethodBase.IsVirtual


Gets a value indicating whether the method is virtual.
MemberType
(inherited from System.Reflection.MethodInfo)
Read-only

See base class member description: System.Reflection.MethodInfo.MemberType


Gets a value indicating that this member is a method.
MethodHandle Read-only

Overridden:
Retrieves the internal handle for the method. Use this handle to access the underlying metadata handle.
Name Read-only

Overridden:
Retrieves the name of this method.
ReflectedType Read-only

Overridden:
Retrieves the class that was used in reflection to obtain this object.
ReturnType Read-only

Overridden:
Retrieves the type of this method's return value.
ReturnTypeCustomAttributes Read-only

Overridden:
Returns the custom attributes of the method's return type.
Signature Read-only

Retrieves the signature of the field.
Public Methods
AddDeclarativeSecurity Adds declarative security to this method.
CreateMethodBody Creates the body of the method using the given array of Microsoft intermediate language (MSIL) instructions.
DefineParameter Defines a parameter of this method.
Equals Overridden:
Determines whether the given object is equal to this instance.
GetBaseDefinition Overridden:
Return the base implementation for a method.
GetCustomAttributes Overloaded:
GetCustomAttributes(bool inherit)

Overridden:
Returns all the custom attributes defined for this method.
GetCustomAttributes Overloaded:
GetCustomAttributes(Type attributeType, bool inherit)

Overridden:
Returns the custom attributes identified by the given type.
GetHashCode Overridden:
Gets the hash code for this method.
GetILGenerator Overloaded:
GetILGenerator()

Returns an ILGenerator for this method with a default Microsoft intermediate language (MSIL) stream size of 64 bytes.
GetILGenerator Overloaded:
GetILGenerator(int size)

Returns an ILGenerator for this method with the specified Microsoft intermediate language (MSIL) stream size.
GetMethodImplementationFlags Overridden:
Returns the implementation flags for the method.
GetModule Returns a reference to the module that contains this method.
GetParameters Overridden:
Returns the parameters of this method.
GetToken Returns the MethodToken that represents the token for this method.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
Invoke
(inherited from System.Reflection.MethodBase)
Overloaded:
Invoke(object obj, object[] parameters)

See base class member description: System.Reflection.MethodBase.Invoke


Invokes the underlying method or constructor represented by this MethodInfo object with the specified parameters.
Invoke Overloaded:
Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)

Overridden:
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.
IsDefined Overridden:
Checks if the specified custom attribute type is defined.
SetCustomAttribute Overloaded:
SetCustomAttribute(CustomAttributeBuilder customBuilder)

Sets a custom attribute using a custom attribute builder.
SetCustomAttribute Overloaded:
SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)

Sets a custom attribute using a specified custom attribute blob.
SetImplementationFlags Sets the implementation flags for this method.
SetMarshal Sets marshaling information for the return type of this method.
SetSymCustomAttribute Set a symbolic custom attribute using a blob.
ToString Overridden:
Returns this MethodBuilder instance as a string.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Reflection.Emit.MethodBuilder Member Details

Overridden Property: Attributes (read-only)
Summary
Retrieves the attributes for this method.
C# Syntax:
public override MethodAttributes Attributes {get;}

Return to top


Overridden Property: CallingConvention (read-only)
Summary
Returns the calling convention of the method.
C# Syntax:
public override CallingConventions CallingConvention {get;}

Return to top


Overridden Property: DeclaringType (read-only)
Summary
Returns the type that declares this method.
C# Syntax:
public override Type DeclaringType {get;}

Return to top


Property: InitLocals (read-write)
Summary
Gets or sets whether the local variables in this method should be zero initialized. The default value of this property is true.
C# Syntax:
public bool InitLocals {get; set;}
Remarks
If this property is set to false, the runtime will generate unverifiable code.

Return to top


Property: IsAbstract (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsAbstract

Summary
Gets a value indicating whether the method is abstract.
C# Syntax:
public bool IsAbstract {get;}
Remarks
An abstract member is declared on a base class and has no implementation supplied.

To get the MethodBase, first get the type. From the type, get the method. From the method, get the MethodBase. If the MethodBase or constructor is other than public, it is protected and cannot be readily accessed. To access a non-public method, set the BindingFlags mask to NonPublic in GetMethod.

Example
 class methodbase
 {
    public static int Main(string[] args)
    {      
       Console.WriteLine ("\nReflection.MethodBase");
       
       //Get the MethodBase of two methods.
 
       //Get the types
       Type MyType1 = Type.GetType("System.Runtime.Serialization.Formatter");       
       Type MyType2 = Type.GetType("System.Reflection.MethodBase");
 
       //Get and display the methods
       MethodBase Mymethodbase1 = 
          MyType1.GetMethod("WriteInt32", BindingFlags.NonPublic|BindingFlags.Instance);

       MethodBase Mymethodbase2 = 
          MyType2.GetMethod("GetCurrentMethod", BindingFlags.Public|BindingFlags.Static);
 
       Console.Write("\nMymethodbase = " + Mymethodbase1.ToString());
       if (Mymethodbase1.IsAbstract)
          Console.Write ("\nMymethodbase is an abstract method");
       else
          Console.Write ("\nMymethodbase is not an abstract method");
 
       Console.Write("\n\nMymethodbase = " + Mymethodbase2.ToString());
       if (Mymethodbase2.IsAbstract)
          Console.Write ("\nMymethodbase is an abstract method");
       else
          Console.Write ("\nMymethodbase is not an abstract method");
       
       return 0;
    }
 }
 /*
 Produces the following output
 Reflection.MethodBase
 
 Mymethodbase = Void WriteInt32 (Int32, System.String)
 Mymethodbase is an abstract method
 
 Mymethodbase = System.Reflection.MethodBase GetCurrentMethod ()
 Mymethodbase is not an abstract method
 */

    
See also:
Boolean | BindingFlags

Return to top


Property: IsAssembly (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsAssembly

Summary
Gets a value indicating whether this method can be called by other classes in the same assembly.
C# Syntax:
public bool IsAssembly {get;}
Remarks
If set, this method can be called by other classes in the same assembly.

To get the MethodBase, first get the type. From the type, get the method. From the method, get the MethodBase. If the MethodBase or constructor is other than public, it is protected and cannot be readily accessed. To access a non-public method, set the BindingFlags mask to NonPublic in GetMethod.

Example
 class methodbase
 {
	internal void f() { }
    public static int Main(string[] args)
    { 
       Console.WriteLine ("\nReflection.MethodBase");
       
       //Get the MethodBase of two methods.
 
       //Get the types
       Type MyType1 = Type.GetType("System.Runtime.Serialization.Formatter");
       Type MyType2 = Type.GetType("methodbase");
 
       //Get and display the methods and the IsAssembly
       MethodBase Mymethodbase1 = 
          MyType1.GetMethod("WriteInt32",BindingFlags.NonPublic|BindingFlags.Instance);
       MethodBase Mymethodbase2 = 
          MyType2.GetMethod("f", BindingFlags.NonPublic|BindingFlags.Instance);
 
       Console.Write("\nMymethodbase = " + Mymethodbase1.ToString());
       if (Mymethodbase1.IsAssembly)
          Console.Write ("\nMymethodbase is an assembly method");
       else
          Console.Write ("\nMymethodbase is not an assembly method");
 
       Console.Write("\n\nMymethodbase = " + Mymethodbase2.ToString());
       if (Mymethodbase2.IsAssembly)
          Console.Write ("\nMymethodbase is an assembly method");
       else
          Console.Write ("\nMymethodbase is not an assembly method");
       
       return 0;
    }
 }
 /*
 Produces the following output
 Reflection.MethodBase
 
 Mymethodbase = Void WriteInt32 (Int32, System.String)
 Mymethodbase is not an assembly method
 
 Mymethodbase = Void f()
 Mymethodbase is an assembly method
 */

    
See also:
FieldAttributes | Boolean | BindingFlags

Return to top


Property: IsConstructor (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsConstructor

Summary
Gets a value indicating whether the method is a constructor.
C# Syntax:
public bool IsConstructor {get;}
See also:
Boolean

Return to top


Property: IsFamily (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsFamily

Summary
Gets a value indicating whether access to this method is restricted to members of the class and members of its derived classes.
C# Syntax:
public bool IsFamily {get;}
Remarks
If a type member has Family level visibility it can be called from any member in a derived class, but not from any other type.
See also:
Boolean

Return to top


Property: IsFamilyAndAssembly (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsFamilyAndAssembly

Summary
Gets a value indicating whether this method can be called by derived classes if they are in the same assembly.
C# Syntax:
public bool IsFamilyAndAssembly {get;}
See also:
Boolean

Return to top


Property: IsFamilyOrAssembly (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsFamilyOrAssembly

Summary
Gets a value indicating whether this method can be called by derived classes, wherever they are, and by all classes in the same assembly.
C# Syntax:
public bool IsFamilyOrAssembly {get;}
Remarks
If a type member has FamilyOrAssembly level visibility it can be called from any member in a derived class or any member in the same assembly, but not from any other type.
See also:
Boolean

Return to top


Property: IsFinal (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsFinal

Summary
Gets a value indicating whether this method is final.
C# Syntax:
public bool IsFinal {get;}
Remarks
To determine if a method is overridable, it is not sufficient to check that MethodBase.IsVirtual is true. For a method to be overridable, IsVirtual must be true and IsFinal must be false. For example, a method might be non-virtual, but it implements an interface method. The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method virtualfinal. So there are cases where a method is marked as virtual but is still not overridable.

To establish with certainty whether a method is overridable, use code such as this:

if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)

If IsVirtual is false or IsFinal is true, then the method cannot be overridden.

Example
This example prints false for IsFinal, which might lead you to think that MyMethod is overridable. The code prints false even though MyMethod is not marked virtual and thus cannot be overridden.
 using System;
 using System.Reflection;
 
 public class MyClass 
 {
 public void MyMethod() 
   {
   }
 public static void Main() 
   {
    MethodBase m = typeof(MyClass).GetMethod("MyMethod");
    Console.WriteLine(m.IsFinal);
   }
 }

    
See also:
Boolean

Return to top


Property: IsHideBySig (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsHideBySig

Summary
Gets a value indicating whether only a member of the same kind with exactly the same signature is hidden in the derived class.
C# Syntax:
public bool IsHideBySig {get;}

Return to top


Property: IsPrivate (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsPrivate

Summary
Gets a value indicating whether this member is private.
C# Syntax:
public bool IsPrivate {get;}
Remarks
If a type member has Private level visibility, it can be called from any member in the same class and no others.
See also:
Boolean

Return to top


Property: IsPublic (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsPublic

Summary
Gets a value indicating whether this is a public method.
C# Syntax:
public bool IsPublic {get;}
Remarks
To get the MethodBase, first get the type. From the type, get the method. From the method, get the MethodBase. If the MethodBase or constructor is other than public, it is protected and cannot be readily accessed. To access a non-public method, set the BindingFlags mask to NonPublic in GetMethod.
Example
 class methodbase
 {
    public static int Main(string[] args)
    {     
 
       Console.WriteLine("\nReflection.MethodBase");
       
       //Get the MethodBase of a method.
  
       //Get the type
       Type MyType = Type.GetType("System.MulticastDelegate");
  
       //Get and display the method
       MethodBase Mymethodbase =
          MyType.GetMethod("RemoveImpl",BindingFlags.NonPublic);
  
       Console.Write("\nMymethodbase = " + Mymethodbase);
  
       bool Myispublic = Mymethodbase.IsPublic;
       if (Myispublic)
          Console.Write ("\nMymethodbase is a public method");
       else
          Console.Write ("\nMymethodbase is not a public method");
       
       return 0;
    }
 }
 /*
 Produces the following output
 
 Reflection.MethodBase
 Mymethodbase = System.Delegate RemoveImpl (System.Delegate)
 Mymethodbase is not a public method
 */

    
See also:
Boolean | BindingFlags

Return to top


Property: IsSpecialName (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsSpecialName

Summary
Gets a value indicating whether this method has a special name.
C# Syntax:
public bool IsSpecialName {get;}
Remarks
The MethodAttributes.SpecialName bit is set to flag members that are treated in a special way by some compilers (such as property accessors and operator overloading methods).
See also:
Boolean | MethodAttributes

Return to top


Property: IsStatic (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsStatic

Summary
Gets a value indicating whether the method is static.
C# Syntax:
public bool IsStatic {get;}
Remarks
A static member cannot implicitly reference instance data in a class (see MethodAttributes.Static).
See also:
Boolean | MethodAttributes

Return to top


Property: IsVirtual (read-only)
Inherited
See base class member description: System.Reflection.MethodBase.IsVirtual

Summary
Gets a value indicating whether the method is virtual.
C# Syntax:
public bool IsVirtual {get;}
Remarks
A virtual member may reference instance data in a class and must be referenced through an instance of the class.

To determine if a method is overridable, it is not sufficient to check that IsVirtual is true. For a method to be overridable, IsVirtual must be true and MethodBase.IsFinal must be false. For example, a method might be non-virtual, but it implements an interface method. The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method virtualfinal. So there are cases where a method is marked as virtual but is still not overridable.

To establish with certainty whether a method is overridable, use code such as this:

if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)

If IsVirtual is false or IsFinal is true, then the method cannot be overridden.

Example
This example prints false for IsFinal, which might lead you to think that MyMethod is overridable. The code prints false even though MyMethod is not marked virtual and thus cannot be overridden.
 using System;
 using System.Reflection;
 
 public class MyClass 
 {
 public void MyMethod() 
   {
   }
 public static void Main() 
   {
    MethodBase m = typeof(MyClass).GetMethod("MyMethod");
    Console.WriteLine(m.IsFinal);
   }
 }

    

Return to top


Property: MemberType (read-only)
Inherited
See base class member description: System.Reflection.MethodInfo.MemberType

Summary
Gets a value indicating that this member is a method.
C# Syntax:
public override MemberTypes MemberType {get;}
Remarks
This is used when this method is being tested as generic member.

To get the MemberType property, first get the class Type. From the Type, get the MethodInfo. From the MethodInfo, get the MemberType.

Example
using System;
using System.Reflection;
 
 class MyMethodInfo
 {
   public static int Main()
   {
       Console.WriteLine("Reflection.MethodInfo");
  
       //Get the Type and MethodInfo.
       Type MyType = Type.GetType("System.Reflection.FieldInfo");
       MethodInfo Mymethodinfo = MyType.GetMethod("GetValue");
       Console.WriteLine(MyType.FullName + "." + Mymethodinfo.Name);
  
       //Get and display the MemberType property.
       MemberTypes Mymembertypes = Mymethodinfo.MemberType;
      
 
       if ( MemberTypes.Constructor == Mymembertypes )
       {
          Console.WriteLine( "MemberType is of type All" );
       }
       else if ( MemberTypes.Custom == Mymembertypes )
       {
          Console.WriteLine( "MemberType is of type Custom" );
       }
       else if ( MemberTypes.Event == Mymembertypes )
       {
          Console.WriteLine( "MemberType is of type Event" );
       }
       else if ( MemberTypes.Field == Mymembertypes )
       {
          Console.WriteLine( "MemberType is of type Field" );
       }
       else if ( MemberTypes.Method == Mymembertypes )
       {
          Console.WriteLine( "MemberType is of type Method" );
       }
       else if ( MemberTypes.Property == Mymembertypes )
       {
          Console.WriteLine( "MemberType is of type Property" );
       }
       else if ( MemberTypes.TypeInfo == Mymembertypes )
       {
          Console.WriteLine( "MemberType is of type TypeInfo" );
       }
 
       return 0;
    }
 }
 /*
 This code produces the following output:
 
 Reflection.MethodInfo
 System.Reflection.FieldInfo.GetValue
 MemberType is of type Method
 */

    

Return to top


Overridden Property: MethodHandle (read-only)
Summary
Retrieves the internal handle for the method. Use this handle to access the underlying metadata handle.
C# Syntax:
public override RuntimeMethodHandle MethodHandle {get;}
Exceptions
Exception Type Condition
NotSupportedException This method is not currently supported. Retrieve the method using Type.GetMethod and call MethodBase.MethodHandle on the returned MethodInfo.

Return to top


Overridden Property: Name (read-only)
Summary
Retrieves the name of this method.
C# Syntax:
public override string Name {get;}

Return to top


Overridden Property: ReflectedType (read-only)
Summary
Retrieves the class that was used in reflection to obtain this object.
C# Syntax:
public override Type ReflectedType {get;}

Return to top


Overridden Property: ReturnType (read-only)
Summary
Retrieves the type of this method's return value.
C# Syntax:
public override Type ReturnType {get;}

Return to top


Overridden Property: ReturnTypeCustomAttributes (read-only)
Summary
Returns the custom attributes of the method's return type.
C# Syntax:
public override ICustomAttributeProvider ReturnTypeCustomAttributes {get;}
Remarks
This method always returns null. Get the MethodInfo after the containing Type has been created and invoked MethodInfo.ReturnTypeCustomAttributes on the MethodInfo.

Return to top


Property: Signature (read-only)
Summary
Retrieves the signature of the field.
C# Syntax:
public string Signature {get;}

Return to top


Method: AddDeclarativeSecurity(
   SecurityAction action,
   PermissionSet pset
)
Summary
Adds declarative security to this method.
C# Syntax:
public void AddDeclarativeSecurity(
   SecurityAction action,
   PermissionSet pset
);
Parameters:

action

The security action to be taken (Demand, Assert, and so on).

pset

The set of permissions the action applies to.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException The action is invalid (RequestMinimum, RequestOptional, and RequestRefuse are invalid).
InvalidOperationException The containing type has been created using TypeBuilder.CreateType or if the permission set pset contains an action that was added earlier by AddDeclarativeSecurity.
ArgumentNullException pset is null.

Return to top


Method: CreateMethodBody(
   byte[] il,
   int count
)
Summary
Creates the body of the method using the given array of Microsoft intermediate language (MSIL) instructions.
C# Syntax:
public void CreateMethodBody(
   byte[] il,
   int count
);
Parameters:

il

An array containing valid MSIL instructions. If this parameter is null, the method's body is cleared.

count

The number of valid bytes in the MSIL array. This value is ignored if MSIL is null.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException The count is not within the range of indexes of the supplied MSIL instruction array and il is not null.
InvalidOperationException The containing type was previously created using TypeBuilder.CreateType.

-or-

This method was called previously on this MethodBuilder with a non- nullil argument.

Remarks
This method creates the method's body from il, an array containing MSIL instructions. The number of bytes of valid MSIL is given by count.

Note This is currently not fully supported. User cannot supply the location of token fix ups and exception handlers.

Return to top


Method: DefineParameter(
   int position,
   ParameterAttributes attributes,
   string strParamName
)
Summary
Defines a parameter of this method.
C# Syntax:
public ParameterBuilder DefineParameter(
   int position,
   ParameterAttributes attributes,
   string strParamName
);
Parameters:

position

The position of the parameter in the parameter list. Parameters are indexed beginning with the number 1 for the first parameter.

attributes

The attributes of the parameter.

strParamName

The name of the parameter. The name can be the null string.

Return Value:
Returns a ParameterBuilder object that represents the new parrameter of this method.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The method has no parameters.

-or-

position is less than or equal to zero.

-or-

position is greater than the number of the method's parameters.

InvalidOperationException The containing type was previously created using TypeBuilder.CreateType

Return to top


Overridden Method: Equals(
   object obj
)
Summary
Determines whether the given object is equal to this instance.
C# Syntax:
public override bool Equals(
   object obj
);
Parameters:

obj

The object to compare with this MethodBuilder instance.

Return Value:
true if obj is an instance of MethodBuilder and is equal to this object; otherwise, false.
Remarks
Equality is determined by having the same name, attributes, and signature.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~MethodBuilder();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: GetBaseDefinition()
Summary
Return the base implementation for a method.
C# Syntax:
public override MethodInfo GetBaseDefinition();
Return Value:
The base implementation of this method.
Remarks
This is always this MethodBuilder object.

Return to top


Overloaded Method: GetCustomAttributes(
   bool inherit
)
Summary
Returns all the custom attributes defined for this method.
C# Syntax:
public override object[] GetCustomAttributes(
   bool inherit
);
Parameters:

inherit

Specifies whether to search this member's inheritance chain to find the custom attributes.

Return Value:
Returns an array of objects representing all the custom attributes of this method.
Exceptions
Exception Type Condition
NotSupportedException This method is not currently supported. Retrieve the method using Type.GetMethod and call MemberInfo.GetCustomAttributes on the returned MethodInfo.
Implements:
ICustomAttributeProvider.GetCustomAttributes

Return to top


Overloaded Method: GetCustomAttributes(
   Type attributeType,
   bool inherit
)
Summary
Returns the custom attributes identified by the given type.
C# Syntax:
public override object[] GetCustomAttributes(
   Type attributeType,
   bool inherit
);
Parameters:

attributeType

The custom attribute type.

inherit

Specifies whether to search this member's inheritance chain to find the custom attributes.

Return Value:
Returns an array of objects representing the attributes of this method that are of Type attribute type.
Exceptions
Exception Type Condition
NotSupportedException This method is not currently supported. Retrieve the method using Type.GetMethod and call MemberInfo.GetCustomAttributes on the returned MethodInfo.
Implements:
ICustomAttributeProvider.GetCustomAttributes

Return to top


Overridden Method: GetHashCode()
Summary
Gets the hash code for this method.
C# Syntax:
public override int GetHashCode();
Return Value:
The hash code for this method.

Return to top


Overloaded Method: GetILGenerator()
Summary
Returns an ILGenerator for this method with a default Microsoft intermediate language (MSIL) stream size of 64 bytes.
C# Syntax:
public ILGenerator GetILGenerator();
Return Value:
Returns an ILGenerator object for this method.
Exceptions
Exception Type Condition
InvalidOperationException MethodImplAttributes indicates that the method body is not managed MSIL.

Return to top


Overloaded Method: GetILGenerator(
   int size
)
Summary
Returns an ILGenerator for this method with the specified Microsoft intermediate language (MSIL) stream size.
C# Syntax:
public ILGenerator GetILGenerator(
   int size
);
Parameters:

size

The size of the MSIL stream

Return Value:
Returns an ILGenerator object for this method.
Exceptions
Exception Type Condition
InvalidOperationException MethodImplAttributes indicates that the method body is not managed MSIL.

Return to top


Overridden Method: GetMethodImplementationFlags()
Summary
Returns the implementation flags for the method.
C# Syntax:
public override MethodImplAttributes GetMethodImplementationFlags();
Return Value:
Returns the implementation flags for the method.

Return to top


Method: GetModule()
Summary
Returns a reference to the module that contains this method.
C# Syntax:
public Module GetModule();
Return Value:
Returns a reference to the module that contains this method.

Return to top


Overridden Method: GetParameters()
Summary
Returns the parameters of this method.
C# Syntax:
public override ParameterInfo[] GetParameters();
Return Value:
An array of ParameterInfo objects that represent the parameters of the method.
Exceptions
Exception Type Condition
NotSupportedException This method is not currently supported. Retrieve the method using Type.GetMethod and call GetParameters on the returned MethodInfo.

Return to top


Method: GetToken()
Summary
Returns the MethodToken that represents the token for this method.
C# Syntax:
public MethodToken GetToken();
Return Value:
Returns the MethodToken of this method.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: Invoke(
   object obj,
   object[] parameters
)
Inherited
See base class member description: System.Reflection.MethodBase.Invoke

Summary
Invokes the underlying method or constructor represented by this MethodInfo object with the specified parameters.
C# Syntax:
public object Invoke(
   object obj,
   object[] parameters
);
Parameters:

obj

The instance that created this method.

parameters

An argument list for the invoked method or constructor. This is an array of objects with the same number, order, and type as the parameters of the method or constructor to be invoked. If there are no parameters, this should be null.

If the method or constructor represented by this instance takes a ByRef parameter, there is no special attribute required for that parameter in order to invoke the method or constructor using this function. If the parameters contain an uninitialized object, it is treated as System.Empty, which, with the default binder, can be widened to 0, 0.0 or String.

An argument list for the invoked method or constructor. This is an array of objects with the same number, order, and type as the parameters of the method or constructor to be invoked. If there are no parameters, this should be null.

If the method or constructor represented by this instance takes a ByRef parameter, there is no special attribute required for that parameter in order to invoke the method or constructor using this function. If the parameters contain an uninitialized object, it is treated as System.Empty, which, with the default binder, can be widened to 0, 0.0 or String.

Return Value:
An Object containing the return value of the invoked method, or a re-initialized object in the case of a constructor.
Exceptions
Exception Type Condition
TargetException The obj parameter is null and the method is not static.

-or-

The method is not declared or inherited by the class of obj.

ArgumentException The type of the parameters parameter does not match the signature of the method or constructor reflected by this instance.
MemberAccessException The caller does not have permission to invoke the method or constructor.
TargetInvocationException The invoked method or constructor throws an exception.
TargetParameterCountException The parameters array does not have the correct number of arguments.
MethodAccessException The caller does not have permission to execute the constructor.
Remarks
Use this method to invoke methods with parameters that have default values. To bind to these methods, Reflection requires one of the binding flags DefaultValueBinding, DefaultValueChangeType, or DefaultValueFull to be specified explicitly. This is a requirement even in those cases where a value is passed for a parameter that has a default value.

For example, consider a method such as MyMethod(int x, float y = 2.0). To invoke this method with only the first argument as MyMethod(4), pass one of the above binding flags and pass two arguments, namely, 4 for the first argument and Missing.Value for the second argument. Unless you use Missing.Value, you may not omit optional parameters with the Invoke method. If you must do so, use Type.InvokeMember instead.

This is a convenience method that calls the following Invoke method, passing null in the other parameters. If the invoked method throws an exception, Exception.GetBaseException returns the exception.

To invoke a static method using its MethodInfo object, the first parameter should be null, as shown in the following call:

Object myReturnValue = myMethodInfo.Invoke(null, myParametersArray);

See also:
BindingFlags | Missing

Return to top


Overloaded Method: Invoke(
   object obj,
   BindingFlags invokeAttr,
   Binder binder,
   object[] parameters,
   CultureInfo culture
)
Summary
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.
C# Syntax:
public override object Invoke(
   object obj,
   BindingFlags invokeAttr,
   Binder binder,
   object[] parameters,
   CultureInfo culture
);
Parameters:

obj

The object on which to invoke the specified method. If the method is static, this parameter is ignored.

invokeAttr

This must be a bit flag from BindingFlags: InvokeMethod, NonPublic, and so on.

binder

An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects via reflection. If binder is null, the default binder is used. For more details, see Binder.

parameters

An argument list. This is an array of arguments with the same number, order, and type as the parameters of the method to be invoked. If there are no parameters this should be null.

culture

An instance of CultureInfo used to govern the coercion of types. If this is null, the CultureInfo for the current thread is used. (Note that this is necessary to, for example, convert a String that represents 1000 to a Double value, since 1000 is represented differently by different cultures.)

Return Value:
Returns an object containing the return value of the invoked method.
Exceptions
Exception Type Condition
NotSupportedException This method is not currently supported. Retrieve the method using Type.GetMethod and call Type.InvokeMember on the returned MethodInfo.
Remarks
If the method is static, the obj parameter is ignored. For non-static methods, obj should be an instance of a class that inherits or declares the method and must be the same type as this class. If the method has no parameters, the value of parameters should be null. Otherwise the number, type, and order of elements in the parameters array should be identical to the number, type, and order of parameters for the method reflected by this instance.

Note Access restrictions are ignored for fully-trusted code. That is, private constructors, methods, fields, and properties can be accessed and invoked using Reflection whenever the code is fully-trusted.

Return to top


Overridden Method: IsDefined(
   Type attributeType,
   bool inherit
)
Summary
Checks if the specified custom attribute type is defined.
C# Syntax:
public override bool IsDefined(
   Type attributeType,
   bool inherit
);
Parameters:

attributeType

The custom attribute type.

inherit

Specifies whether to search this member's inheritance chain to find the custom attributes.

Return Value:
true if the specified custom attribute type is defined; otherwise, false.
Exceptions
Exception Type Condition
NotSupportedException This method is not currently supported. Retrieve the method using Type.GetMethod and call MemberInfo.IsDefined on the returned MethodInfo.
Implements:
ICustomAttributeProvider.IsDefined

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: SetCustomAttribute(
   CustomAttributeBuilder customBuilder
)
Summary
Sets a custom attribute using a custom attribute builder.
C# Syntax:
public void SetCustomAttribute(
   CustomAttributeBuilder customBuilder
);
Parameters:

customBuilder

An instance of a helper class to describe the custom attribute.

Exceptions
Exception Type Condition
ArgumentNullException customBuilder is null.

Return to top


Overloaded Method: SetCustomAttribute(
   ConstructorInfo con,
   byte[] binaryAttribute
)
Summary
Sets a custom attribute using a specified custom attribute blob.
C# Syntax:
public void SetCustomAttribute(
   ConstructorInfo con,
   byte[] binaryAttribute
);
Parameters:

con

The constructor for the custom attribute.

binaryAttribute

A byte blob representing the attributes.

Exceptions
Exception Type Condition
ArgumentNullException con or binaryAttribute is null.
Remarks
See the metadata specifications in the Tool Developers Guide for details on how to format binaryAttribute.

Return to top


Method: SetImplementationFlags(
   MethodImplAttributes attributes
)
Summary
Sets the implementation flags for this method.
C# Syntax:
public void SetImplementationFlags(
   MethodImplAttributes attributes
);
Parameters:

attributes

The implementation flags to set.

Exceptions
Exception Type Condition
InvalidOperationException The containing type was previously created using TypeBuilder.CreateType.

Return to top


Method: SetMarshal(
   UnmanagedMarshal unmanagedMarshal
)
Summary
Sets marshaling information for the return type of this method.
C# Syntax:
public void SetMarshal(
   UnmanagedMarshal unmanagedMarshal
);
Parameters:

unmanagedMarshal

Marshaling information for the return type of this method.

Exceptions
Exception Type Condition
InvalidOperationException The containing type was previously created using TypeBuilder.CreateType

Return to top


Method: SetSymCustomAttribute(
   string name,
   byte[] data
)
Summary
Set a symbolic custom attribute using a blob.
C# Syntax:
public void SetSymCustomAttribute(
   string name,
   byte[] data
);
Parameters:

name

The name of the symbolic custom attribute.

data

The byte blob that represents the value of the symbolic custom attribute.

Exceptions
Exception Type Condition
InvalidOperationException The containing type was previously created using TypeBuilder.CreateType.

-or-

The module that contains this method is not a debug module.

Remarks
Unlike the metadata custom attribute, this custom attribute is associated with a symbol writer.

Return to top


Overridden Method: ToString()
Summary
Returns this MethodBuilder instance as a string.
C# Syntax:
public override string ToString();
Return Value:
Returns a string containing the name, attributes, method signature, exceptions, and local signature of this method followed by the current Microsoft intermediate language (MSIL) stream.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.