ModuleBuilder.DefineGlobalMethod Method (String, MethodAttributes, CallingConventions, Type, Type[])

Defines a global method with the specified name, attributes, calling convention, return type, and parameter types.

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

public MethodBuilder DefineGlobalMethod(
	string name,
	MethodAttributes attributes,
	CallingConventions callingConvention,
	Type returnType,
	Type[] parameterTypes
)

Parameters

name
Type: System.String

The name of the method. name cannot contain embedded nulls.

attributes
Type: System.Reflection.MethodAttributes

The attributes of the method. attributes must include Static.

callingConvention
Type: System.Reflection.CallingConventions

The calling convention for the method.

returnType
Type: System.Type

The return type of the method.

parameterTypes
Type: System.Type[]

The types of the method's parameters.

Return Value

Type: System.Reflection.Emit.MethodBuilder
The defined global method.

ExceptionCondition
ArgumentException

The method is not static. That is, attributes does not include Static.

-or-

An element in the Type array is null.

ArgumentNullException

name is null.

InvalidOperationException

CreateGlobalFunctions has been previously called.

You cannot use the global method that this method defines until you call CreateGlobalFunctions.

NoteNote

Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later.

The following code sample illustrates the use of DefineGlobalMethod to create a type-independent method tied to the current ModuleBuilder. After building the global method, CreateGlobalFunctions must be called in order to complete it.

AppDomain currentDomain;
AssemblyName myAssemblyName;
MethodBuilder myMethodBuilder=null;
ILGenerator myILGenerator;

// Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain;
myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = 
   currentDomain.DefineDynamicAssembly
               (myAssemblyName, AssemblyBuilderAccess.RunAndSave);
// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");

// Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder.DefineGlobalMethod
     ("MyMethod1",MethodAttributes.Static|MethodAttributes.Public,
           null,null);
myILGenerator = myMethodBuilder.GetILGenerator();
myILGenerator.EmitWriteLine("Hello World from global method.");
myILGenerator.Emit(OpCodes.Ret);
// Fix up the 'TempModule' module .
myModuleBuilder.CreateGlobalFunctions();

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft