ModuleBuilder.DefineGlobalMethod Method (String, MethodAttributes, Type, array<Type[])
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Defines a global method with the specified name, attributes, return type, and parameter types.
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Function DefineGlobalMethod ( _
name As String, _
attributes As MethodAttributes, _
returnType As Type, _
parameterTypes As Type() _
) As MethodBuilder
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: array<System.Type[]
The types of the method's parameters.
Return Value
Type: System.Reflection.Emit.MethodBuilder
The defined global method.
Exceptions
Exception | Condition |
---|---|
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 nulla null reference (Nothing in Visual Basic). |
ArgumentNullException | name is nulla null reference (Nothing in Visual Basic). |
InvalidOperationException | CreateGlobalFunctions has been previously called. |
Remarks
The global method that this method defines is not usable until you call CreateGlobalFunctions.
Examples
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.
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.