DynamicMethod Class
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Defines and represents a dynamic method that can be compiled, executed, and discarded. Discarded methods are available for garbage collection.
System.Reflection::MemberInfo
System.Reflection::MethodBase
System.Reflection::MethodInfo
System.Reflection.Emit::DynamicMethod
Assembly: mscorlib (in mscorlib.dll)
The DynamicMethod type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | DynamicMethod(String, Type, array<Type>) | Creates an anonymously hosted dynamic method, specifying the method name, return type, and parameter types. |
![]() | DynamicMethod(String, Type, array<Type>, Boolean) | Creates an anonymously hosted dynamic method, specifying the method name, return type, parameter types, and whether just-in-time (JIT) visibility checks should be skipped for types and members accessed by the Microsoft intermediate language (MSIL) of the dynamic method. |
![]() | DynamicMethod(String, Type, array<Type>, Module) | Security Critical. Creates a dynamic method that is global to a module, specifying the method name, return type, parameter types, and module. |
![]() | DynamicMethod(String, Type, array<Type>, Type) | Security Critical. Creates a dynamic method, specifying the method name, return type, parameter types, and the type with which the dynamic method is logically associated. |
![]() | DynamicMethod(String, Type, array<Type>, Module, Boolean) | Security Critical. Creates a dynamic method that is global to a module, specifying the method name, return type, parameter types, module, and whether just-in-time (JIT) visibility checks should be skipped for types and members accessed by the Microsoft intermediate language (MSIL) of the dynamic method. |
![]() | DynamicMethod(String, Type, array<Type>, Type, Boolean) | Security Critical. Creates a dynamic method, specifying the method name, return type, parameter types, the type with which the dynamic method is logically associated, and whether just-in-time (JIT) visibility checks should be skipped for types and members accessed by the Microsoft intermediate language (MSIL) of the dynamic method. |
![]() | DynamicMethod(String, MethodAttributes, CallingConventions, Type, array<Type>, Module, Boolean) | Security Critical. Creates a dynamic method that is global to a module, specifying the method name, attributes, calling convention, return type, parameter types, module, and whether just-in-time (JIT) visibility checks should be skipped for types and members accessed by the Microsoft intermediate language (MSIL) of the dynamic method. |
![]() | DynamicMethod(String, MethodAttributes, CallingConventions, Type, array<Type>, Type, Boolean) | Security Critical. Creates a dynamic method, specifying the method name, attributes, calling convention, return type, parameter types, the type with which the dynamic method is logically associated, and whether just-in-time (JIT) visibility checks should be skipped for types and members accessed by the Microsoft intermediate language (MSIL) of the dynamic method. |
| Name | Description | |
|---|---|---|
![]() | Attributes | Gets the attributes specified when the dynamic method was created. (Overrides MethodBase::Attributes.) |
![]() | CallingConvention | Gets the calling convention specified when the dynamic method was created. (Overrides MethodBase::CallingConvention.) |
![]() | ContainsGenericParameters | Gets a value that indicates whether the generic method contains unassigned generic type parameters. (Inherited from MethodBase.) |
![]() | CustomAttributes | Gets a collection that contains this member's custom attributes. (Inherited from MemberInfo.) |
![]() | DeclaringType | Gets the type that declares the method, which is always nullptr for dynamic methods. (Overrides MemberInfo::DeclaringType.) |
![]() | InitLocals | Gets or sets a value indicating whether the local variables in the method are zero-initialized. |
![]() | IsAbstract | Gets a value that indicates whether the method is abstract (MustInherit in Visual Basic). (Inherited from MethodBase.) |
![]() | IsAssembly | Gets a value that indicates whether the potential visibility of this method or constructor is described by MethodAttributes::Assembly; that is, the method or constructor is visible at most to other types in the same assembly, and is not visible to derived types outside the assembly. (Inherited from MethodBase.) |
![]() | IsConstructor | Gets a value that indicates whether the method is a constructor. (Inherited from MethodBase.) |
![]() | IsFamily | Gets a value that indicates whether the visibility of this method or constructor is described by MethodAttributes::Family; that is, the method or constructor is visible only within its class and derived classes. (Inherited from MethodBase.) |
![]() | IsFamilyAndAssembly | Gets a value that indicates whether the visibility of this method or constructor is described by MethodAttributes::FamANDAssem; that is, the method or constructor can be called by derived classes, but only if they are in the same assembly. (Inherited from MethodBase.) |
![]() | IsFamilyOrAssembly | Gets a value that indicates whether the potential visibility of this method or constructor is described by MethodAttributes::FamORAssem; that is, the method or constructor can be called by derived classes wherever they are, and by classes in the same assembly. (Inherited from MethodBase.) |
![]() | IsFinal | Gets a value that indicates whether this method is final (NotOverridable in Visual Basic). (Inherited from MethodBase.) |
![]() | IsGenericMethod | Gets a value that indicates whether the method is generic. (Inherited from MethodBase.) |
![]() | IsGenericMethodDefinition | Gets a value that indicates whether the method is a generic method definition. (Inherited from MethodBase.) |
![]() | IsHideBySig | Gets a value that indicates whether only a member of the same kind with exactly the same signature is hidden in the derived class. (Inherited from MethodBase.) |
![]() | IsPrivate | Gets a value that indicates whether this member is private. (Inherited from MethodBase.) |
![]() | IsPublic | Gets a value that indicates whether this is a public method. (Inherited from MethodBase.) |
![]() | IsSpecialName | Gets a value that indicates whether this method has a special name. (Inherited from MethodBase.) |
![]() | IsStatic | Gets a value that indicates whether the method is static (Shared in Visual Basic). (Inherited from MethodBase.) |
![]() | IsVirtual | Gets a value that indicates whether the method is virtual (Overridable in Visual Basic). (Inherited from MethodBase.) |
![]() | MemberType | Gets a MemberTypes value indicating that this member is a method. (Inherited from MethodInfo.) |
![]() | MetadataToken | Gets a value that identifies a metadata element. (Inherited from MemberInfo.) |
![]() | MethodHandle | Not supported for dynamic methods. (Overrides MethodBase::MethodHandle.) |
![]() | MethodImplementationFlags | Gets the MethodImplAttributes flags that specify the attributes of a method implementation. (Inherited from MethodBase.) |
![]() | Module | Gets the module with which the dynamic method is logically associated. (Overrides MemberInfo::Module.) |
![]() | Name | Gets the name of the dynamic method. (Overrides MemberInfo::Name.) |
![]() | ReflectedType | Gets the class that was used in reflection to obtain the method. (Overrides MemberInfo::ReflectedType.) |
![]() | ReturnParameter | Gets the return parameter of the dynamic method. (Overrides MethodInfo::ReturnParameter.) |
![]() | ReturnType | Gets the type of return value for the dynamic method. (Overrides MethodInfo::ReturnType.) |
![]() | ReturnTypeCustomAttributes | Gets the custom attributes of the return type for the dynamic method. (Overrides MethodInfo::ReturnTypeCustomAttributes.) |
| Name | Description | |
|---|---|---|
![]() | CreateDelegate(Type) | Completes the dynamic method and creates a delegate of the specified type, which can be used to execute the dynamic method. (Overrides MethodInfo::CreateDelegate(Type).) |
![]() | CreateDelegate(Type, Object) | Completes the dynamic method and creates a delegate that can be used to execute it, specifying the delegate type and an object the delegate is bound to. (Overrides MethodInfo::CreateDelegate(Type, Object).) |
![]() | DefineParameter | Defines a parameter of the dynamic method. |
![]() | Equals | Returns a value that indicates whether this instance is equal to a specified object. (Inherited from MethodInfo.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetBaseDefinition | Returns the base implementation for the method. (Overrides MethodInfo::GetBaseDefinition().) |
![]() | GetCustomAttributes(Boolean) | Returns all the custom attributes defined for the method. (Overrides MemberInfo::GetCustomAttributes(Boolean).) |
![]() | GetCustomAttributes(Type, Boolean) | Returns the custom attributes of the specified type that have been applied to the method. (Overrides MemberInfo::GetCustomAttributes(Type, Boolean).) |
![]() | GetGenericArguments | Returns an array of Type objects that represent the type arguments of a generic method or the type parameters of a generic method definition. (Inherited from MethodInfo.) |
![]() | GetGenericMethodDefinition | Returns a MethodInfo object that represents a generic method definition from which the current method can be constructed. (Inherited from MethodInfo.) |
![]() | GetHashCode | Returns the hash code for this instance. (Inherited from MethodInfo.) |
![]() | GetILGenerator() | Returns a Microsoft intermediate language (MSIL) generator for the method with a default MSIL stream size of 64 bytes. |
![]() | GetILGenerator(Int32) | Returns a Microsoft intermediate language (MSIL) generator for the method with the specified MSIL stream size. |
![]() | GetMethodImplementationFlags | Returns the implementation flags for the method. (Overrides MethodBase::GetMethodImplementationFlags().) |
![]() | GetParameters | Returns the parameters of the dynamic method. (Overrides MethodBase::GetParameters().) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | Invoke(Object, array<Object>) | Invokes the method or constructor represented by the current instance, using the specified parameters. (Inherited from MethodBase.) |
![]() | Invoke(Object, BindingFlags, Binder, array<Object>, CultureInfo) | Invokes the dynamic method using the specified parameters, under the constraints of the specified binder, with the specified culture information. (Overrides MethodBase::Invoke(Object, BindingFlags, Binder, array<Object>, CultureInfo).) |
![]() | IsDefined | Indicates whether the specified custom attribute type is defined. (Overrides MemberInfo::IsDefined(Type, Boolean).) |
![]() | MakeGenericMethod | Substitutes the elements of an array of types for the type parameters of the current generic method definition, and returns a MethodInfo object representing the resulting constructed method. (Inherited from MethodInfo.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns the signature of the method, represented as a string. (Overrides Object::ToString().) |
| Name | Description | |
|---|---|---|
![]() | GetCustomAttribute(Type) | Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member. (Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttribute(Type, Boolean) | Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member. (Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttribute<T>() | Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member. (Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttribute<T>(Boolean) | Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member. (Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttributes() | Overloaded. Retrieves a collection of custom attributes that are applied to a specified member. (Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttributes(Type) | Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member. (Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttributes<T>() | Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member. (Defined by CustomAttributeExtensions.) |
![]() | GetCustomAttributes<T>(Boolean) | Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member, and optionally inspects the ancestors of that member. (Defined by CustomAttributeExtensions.) |
![]() | GetRuntimeBaseDefinition | Retrieves an object that represents the specified method on the direct or indirect base class where the method was first declared. (Defined by RuntimeReflectionExtensions.) |
![]() | IsDefined | Indicates whether custom attributes of a specified type are applied to a specified member. (Defined by CustomAttributeExtensions.) |
You can use the DynamicMethod class to generate and execute a method at run time, without having to generate a dynamic assembly and a dynamic type to contain the method. The executable code created by the just-in-time (JIT) compiler is reclaimed when the DynamicMethod object is reclaimed. Dynamic methods are the most efficient way to generate and execute small amounts of code.
In Windows Phone applications, all dynamic methods are anonymously hosted. Anonymously hosted dynamic methods are located in a system-provided assembly, and therefore are isolated from other code. They do not have access to any non-public data.
Dynamic methods and their parameters do not have to be named, but you can specify names to assist in debugging. Custom attributes are not supported on dynamic methods or their parameters.
Note: |
|---|
Dynamic methods do not support symbol information, that is, local variable names and line-number mapping. This limitation might be removed in a future version. |
Although dynamic methods behave like static methods (Shared methods in Visual Basic), the rules for delegate binding allow a dynamic method to be bound to an object, so that it acts like an instance method when it is called using that delegate instance. An example that demonstrates this is provided for the CreateDelegate(Type, Object) method overload. For more information about delegate binding rules, see the Delegate class.
The following example creates a dynamic method that takes two parameters. The example emits a simple function body that returns a formatted string. The first parameter is a string that the method embeds in the formatted string; the second parameter is an integer, which the method squares and then embeds in the formatted string. The example completes the method by creating a delegate, invokes the delegate with different parameters, and finally invokes the dynamic method by using the Invoke method.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |




Note: