System.Reflection.Emit.MethodBuilder class

This article provides supplementary remarks to the reference documentation for this API.

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

You can use reflection emit to define global methods and to define methods as type members. The APIs that define methods return MethodBuilder objects.

Global methods

A global method is defined by using the ModuleBuilder.DefineGlobalMethod method, which returns a MethodBuilder object.

Global methods must be static. If a dynamic module contains global methods, the ModuleBuilder.CreateGlobalFunctions method must be called before persisting the dynamic module or the containing dynamic assembly because the common language runtime postpones fixing up the dynamic module until all global functions have been defined.

A global native method is defined by using the ModuleBuilder.DefinePInvokeMethod method. Platform invoke (PInvoke) methods must not be declared abstract or virtual. The runtime sets the MethodAttributes.PinvokeImpl attribute for a platform invoke method.

Methods as members of types

A method is defined as a type member by using the TypeBuilder.DefineMethod method, which returns a MethodBuilder object.

The DefineParameter method is used to set the name and parameter attributes of a parameter, or of the return value. The ParameterBuilder object returned by this method represents a parameter or the return value. The ParameterBuilder object can be used to set the marshaling, to set the constant value, and to apply custom attributes.

Attributes

Members of the MethodAttributes enumeration define the precise character of a dynamic method:

Known issues