This topic has not yet been rated - Rate this topic

DynamicMethod.Invoke Method (Object, BindingFlags, Binder, Object[], CultureInfo)

Note: This method is new in the .NET Framework version 2.0.

Invokes the dynamic method using the specified parameters, under the constraints of the specified binder, with the specified culture information.

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

public override Object Invoke (
	Object obj,
	BindingFlags invokeAttr,
	Binder binder,
	Object[] parameters,
	CultureInfo culture
)
public Object Invoke (
	Object obj, 
	BindingFlags invokeAttr, 
	Binder binder, 
	Object[] parameters, 
	CultureInfo culture
)
public override function Invoke (
	obj : Object, 
	invokeAttr : BindingFlags, 
	binder : Binder, 
	parameters : Object[], 
	culture : CultureInfo
) : Object

Parameters

obj

This parameter is ignored for dynamic methods, because they are static. Specify a null reference (Nothing in Visual Basic).

invokeAttr

A bitwise combination of BindingFlags values.

binder

A Binder object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects through reflection. If binder is a null reference (Nothing in Visual Basic), the default binder is used. For more details, see Binder.

parameters

An argument list. This is an array of arguments with the same number, order, and type as the parameters of the method to be invoked. If there are no parameters this parameter should be a null reference (Nothing in Visual Basic).

culture

An instance of CultureInfo used to govern the coercion of types. If this is a null reference (Nothing in Visual Basic), the CultureInfo for the current thread is used. For example, this information is needed to correctly convert a String that represents 1000 to a Double value, because 1000 is represented differently by different cultures.

Return Value

A Object containing the return value of the invoked method.
Exception type Condition

NotSupportedException

The CallingConventions.VarArgs calling convention is not supported.

TargetParameterCountException

The number of elements in parameters does not match the number of parameters in the dynamic method.

ArgumentException

The type of one or more elements of parameters does not match the type of the corresponding parameter of the dynamic method.

In addition to the listed exceptions, the calling code should be prepared to catch any exceptions thrown by the dynamic method.

Executing a dynamic method with a delegate created by the CreateDelegate method is more efficient than executing it with the Invoke method.

Calling the Invoke method or the CreateDelegate method completes the dynamic method. Any further attempt to alter the dynamic method, such as modifying parameter definitions or emitting more Microsoft intermediate language (MSIL), is ignored; no exception is thrown.

All dynamic methods are static, so the obj parameter is always ignored. To treat a dynamic method as if it were an instance method, use the CreateDelegate(Type,Object) overload that takes an object instance.

If the dynamic method has no parameters, the value of parameters should be a null reference (Nothing in Visual Basic). Otherwise the number, type, and order of elements in the parameters array should be identical to the number, type, and order of parameters of the dynamic method.

NoteNote

This method overload is called by the Invoke(Object,Object[]) method overload inherited from the MethodBase class, so the preceding remarks apply to both overloads.

The following code example invokes a dynamic method with exact binding, using the US-English culture. This code example is part of a larger example provided for the DynamicMethod class.

Console.WriteLine("\r\nUse the Invoke method to execute the dynamic method:");
// Create an array of arguments to use with the Invoke method.
object[] invokeArgs = {"\r\nHello, World!", 42};
// Invoke the dynamic method using the arguments. This is much
// slower than using the delegate, because you must create an
// array to contain the arguments, and value-type arguments
// must be boxed.
object objRet = hello.Invoke(null, BindingFlags.ExactBinding, null, invokeArgs, new CultureInfo("en-us"));
Console.WriteLine("hello.Invoke returned: " + objRet);

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ