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

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
)

Parameters

obj
Type: System.Object

This parameter is ignored for dynamic methods, because they are static. Specify null.

invokeAttr
Type: System.Reflection.BindingFlags

A bitwise combination of BindingFlags values.

binder
Type: System.Reflection.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 null, the default binder is used. For more details, see Binder.

parameters
Type: System.Object[]

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

culture
Type: System.Globalization.CultureInfo

An instance of CultureInfo used to govern the coercion of types. If this is null, 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

Type: System.Object
A Object containing the return value of the invoked method.

Implements

_MethodBase.Invoke(Object, BindingFlags, Binder, Object[], CultureInfo)

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

TargetInvocationException

The dynamic method is associated with a module, is not anonymously hosted, and was constructed with skipVisibility set to false, but the dynamic method accesses members that are not public or internal (Friend in Visual Basic).

-or-

The dynamic method is anonymously hosted and was constructed with skipVisibility set to false, but it accesses members that are not public.

-or-

The dynamic method contains unverifiable code. See the "Verification" section in Remarks for DynamicMethod.

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

This method does not demand permissions directly, but invoking the dynamic method can result in security demands, depending on the method. For example, no demands are made for anonymously hosted dynamic methods that are created with the restrictedSkipVisibility parameter set to false. On the other hand, if you create a method with restrictedSkipVisibility set to true so it can access a hidden member of a target assembly, the method will cause a demand for the permissions of the target assembly plus ReflectionPermission with the ReflectionPermissionFlag.MemberAccess flag.

NoteNote

Prior to the .NET Framework 2.0 Service Pack 1, this method required ReflectionPermission with the MemberAccess flag.

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);

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft