共用方式為


ModuleBuilder.CreateGlobalFunctions Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Sub CreateGlobalFunctions
public void CreateGlobalFunctions()

Exceptions

Exception Condition
InvalidOperationException

This method was called previously.

Remarks

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.

Examples

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.

Imports System.Reflection
Imports System.Reflection.Emit

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Get the current application domain.
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain

      ' Define a dynamic assembly and a dynamic module.
      Dim aName As New AssemblyName("TempAssembly")
      Dim ab As AssemblyBuilder = _
         currentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run)
      Dim mb As ModuleBuilder = _
         ab.DefineDynamicModule("TempModule")

      ' Define a global method in the module. The method has no parameters,
      ' and returns a constant string.
      Dim globalMethod As MethodBuilder = _
         mb.DefineGlobalMethod("GetGreeting", _
                               MethodAttributes.Static Or MethodAttributes.Public, _
                               GetType(String), Nothing)

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

      ' Complete the global function.
      mb.CreateGlobalFunctions()

      ' Define a public type with a Shared method named M1. The method has no 
      ' parameters, and returns a string.
      Dim tb As TypeBuilder = mb.DefineType("TestType", TypeAttributes.Public)
      Dim m1 As MethodBuilder = _
         tb.DefineMethod("M1", _
                         MethodAttributes.Public Or MethodAttributes.Static, _
                         GetType(String), Nothing)

      ' 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.
      Dim t As Type = tb.CreateType()
      outputBlock.Text &= "M1 returned: " & _
         t.InvokeMember("M1", _
                        BindingFlags.InvokeMethod Or BindingFlags.Public _
                                                  Or BindingFlags.Static, _
                        Nothing, Nothing, Nothing).ToString() & vbLf

      ' Try to call the global method directly.
      Dim miGlobal As MethodInfo = mb.GetMethod("GetGreeting")
      outputBlock.Text &= "Invoking the global method:" & vbCrLf
      Try
         outputBlock.Text &= "GetGreeting returned: " & miGlobal.Invoke(Nothing, Nothing)
      Catch ex As Exception
         outputBlock.Text &= ex.GetType().Name & ": " & ex.Message & vbLf
      End Try

   End Sub 

End Class 

' This example produces output similar to the following:
'
'M1 returned: Hello, World!
'Invoking the global method:
'MethodAccessException: <Module>.GetGreeting()
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()
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.