共用方式為


Defining a Method with Reflection Emit 

This section describes how to use reflection emit to define global methods and to define methods as type members. The APIs that define methods return MethodBuilder objects.

The MethodBuilder.AddDeclarativeSecurity method adds declarative security to a method being built. AddDeclarativeSecurity can be called several times with each call specifying a security action (such Demand, Assert, Deny) and a set of permissions that the action applies to.

A global method is defined using the ModuleBuilder.DefineGlobalMethod method. DefineGlobalMethod returns a MethodBuilder.

Global methods must be static. If a dynamic module contains global methods, the ModuleBuilder.CreateGlobalFunctions method must be called before persisting the dynamic module or the containing dynamic assembly because the common language runtime postpones fixing up the dynamic module until all global functions have been defined.

A global native method is defined using the method ModuleBuilder.DefinePInvokeMethod. Platform invoke (PInvoke) methods must not be declared abstract or virtual. The runtime sets the MethodAttributes.PInvokeImpl attribute for a platform invoke method.

A method is defined as a type member using the TypeBuilder.DefineMethod method. DefineMethod returns a MethodBuilder.

The DefineParameter method is used to set the name and parameter attributes of a parameter, or of the return value. The ParameterBuilder object returned by this method represents a parameter or the return value. ParameterBuilder object can be used to set the marshaling, to set the constant value, and to apply custom attributes.

NoteNote

In the .NET Framework versions 1.0 and 1.1, the DefineParameter method cannot be used on the return value.

Attributes

  • Static methods are specified using the MethodAttributes.Static attribute.

  • Final methods, (methods that cannot be overridden) are specified using the MethodAttributes.Final attribute.

  • Virtual methods are specified using the MethodAttributes.Virtual attribute.

  • Abstract methods are specified using the MethodAttributes.Abstract attribute.

  • Several attributes determine method visibility. See the description of the MethodAttributes enumeration.

  • Methods that implement overloaded operators must set the MethodAttributes.SpecialName attribute.

  • Finalizers must set the MethodAttributes.SpecialName attribute.

Known Issues

  • Although MethodBuilder is derived from MethodInfo, some of the abstract methods defined in the MethodInfo class are not fully implemented in MethodBuilder. These MethodBuilder methods throw the NotSupportedException. For example the MethodBuilder.Invoke method is not fully implemented. You can reflect on these methods by retrieving the enclosing type using Type.GetType or Assembly.GetType.

  • Custom modifiers are supported in the .NET Framework version 2.0. They are not supported in earlier versions.

See Also

Other Resources

Using Reflection Emit