ModuleBuilder.CreateGlobalFunctions Method

Completes the global function definitions and global data definitions for this dynamic module.

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

public void CreateGlobalFunctions()

ExceptionCondition
InvalidOperationException

This method was called previously.

Important noteImportant Note:

In Silverlight, global methods can be called only by code in the dynamic module.

This method should be called when the user is done with defining all the global functions within this dynamic module. After calling this function, no more new global functions or new global data are allowed.

The following example illustrates the use of CreateGlobalFunctions and ModuleBuilder.DefineGlobalMethod to create a static global method named GetGreeting that returns a constant string. The example creates a type named TestType with a static method M1 that calls GetGreeting. M1 simply returns this string.

The example then attempts to invoke GetGreeting directly, catches the resulting exception, and displays 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 = 
         mb.DefineGlobalMethod("GetGreeting", 
                               MethodAttributes.Static | MethodAttributes.Public, 
                               typeof(string), null);

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

      // Complete the global function.
      mb.CreateGlobalFunctions();

      // 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);
      il.Emit(OpCodes.Ret);

      // Complete the type, and invoke M1.
      Type t = tb.CreateType();
      outputBlock.Text += "M1 returned: " + 
         t.InvokeMember("M1", 
                        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";
      try
      {
         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()
 */


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.

Community Additions

ADD
Show: