Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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
)

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.
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-

The length of name is zero

-or-

An element in the Type array is null.

ArgumentNullException

name is null.

InvalidOperationException

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 = 
         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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.