Export (0) Print
Expand All

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

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

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

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


Type: System.String
The name of the method. name cannot contain embedded nulls.
Type: System.Reflection.MethodAttributes
The attributes of the method. attributes must include Static.
Type: System.Type
The return type of the method.
Type: System.Type[]
The types of the method's parameters.

Return Value

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


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


The length of name is zero


An element in the Type array is null.


name is null.


CreateGlobalFunctions has been previously called.

The global method that this method defines is not usable until you call CreateGlobalFunctions.

The following example 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.

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

public class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      // Get the current application domain.
      AppDomain currentDomain = AppDomain.CurrentDomain;

      // Define a dynamic assembly and a dynamic module.
      AssemblyName aName = new AssemblyName("TempAssembly");
      AssemblyBuilder ab = 
         currentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
      ModuleBuilder mb = ab.DefineDynamicModule("TempModule");

      // Define a global method in the module. The method has no parameters,
      // and returns a constant string.
      MethodBuilder globalMethod = 
                               MethodAttributes.Static | MethodAttributes.Public, 
                               typeof(string), null);

      ILGenerator il = globalMethod.GetILGenerator();
      il.Emit(OpCodes.Ldstr, "Hello, World!");

      // Complete the global function.

      // Define a public type with a static method named M1. The method has no 
      // parameters, and returns a string.
      TypeBuilder tb = mb.DefineType("TestType", TypeAttributes.Public);
      MethodBuilder m1 = tb.DefineMethod("M1", 
                                         MethodAttributes.Public | MethodAttributes.Static, 
                                         typeof(string), null);

      // In the method body of M1, call the global method "GetGreeting". This 
      // puts the return value of GetGreeting on the execution stack. Return
      // that value. 
      il = m1.GetILGenerator();
      il.Emit(OpCodes.Call, globalMethod);

      // Complete the type, and invoke M1.
      Type t = tb.CreateType();
      outputBlock.Text += "M1 returned: " + 
                        BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, 
                        null, null, null).ToString() + "\n";

      // Try to call the global method directly.
      MethodInfo miGlobal = mb.GetMethod("GetGreeting");
      outputBlock.Text += "Invoking the global method:\n";
         outputBlock.Text += "GetGreeting returned: " + miGlobal.Invoke(null, null);
      catch(Exception ex)
         outputBlock.Text += ex.GetType().Name + ": " + ex.Message;

/* This example produces output similar to the following:

M1 returned: Hello, World!
Invoking the global method:
MethodAccessException: <Module>.GetGreeting()


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.

Community Additions

© 2015 Microsoft