System.Reflection.Emit.DynamicMethod.Invoke Method

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

Syntax

public override object Invoke (object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture)

Parameters

obj
This parameter is ignored for dynamic methods, because they are static. Specify null.
invokeAttr
A bitwise combination of System.Reflection.BindingFlags values.
binder
A System.Reflection.Binder object that enables the binding, coercion of argument types, invocation of members, and retrieval of System.Reflection.MemberInfo objects through reflection. If binder is null, the default binder is used. For more details, see System.Reflection.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 null.
culture
An instance of System.Globalization.CultureInfo used to govern the coercion of types. If this is null, the System.Globalization.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.

Returns

A object containing the return value of the invoked method.

Remarks

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 erload:System.Reflection.Emit.DynamicMethod.CreateDelegate method is more efficient than executing it with the DynamicMethod.Invoke(object, System.Reflection.BindingFlags, System.Reflection.Binder, Object[], System.Globalization.CultureInfo) method.

Calling the DynamicMethod.Invoke(object, System.Reflection.BindingFlags, System.Reflection.Binder, Object[], System.Globalization.CultureInfo) method or the DynamicMethod.CreateDelegate(Type) 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 DynamicMethod.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.

Note:

This method overload is called by the System.Reflection.MethodBase.Invoke(object, Object[]) method overload inherited from the System.Reflection.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 System.Security.Permissions.ReflectionPermission with the System.Security.Permissions.ReflectionPermissionFlag.MemberAccess flag.

Note:

Prior to the net_v20sp1_long, this method required System.Security.Permissions.ReflectionPermission with the System.Security.Permissions.ReflectionPermissionFlag.MemberAccess flag.

Requirements

Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0