TypeBuilder.DefineMethod Method (String, MethodAttributes)
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Adds a new method to the type, with the specified name and method attributes.
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Function DefineMethod ( _
name As String, _
attributes As MethodAttributes _
) As MethodBuilder
public MethodBuilder DefineMethod(
string name,
MethodAttributes attributes
)
Parameters
- name
Type: System.String
The name of the method. name cannot contain embedded nulls.
- attributes
Type: System.Reflection.MethodAttributes
The attributes of the method.
Return Value
Type: System.Reflection.Emit.MethodBuilder
The defined method.
Exceptions
Exception | Condition |
---|---|
ArgumentException | The length of name is zero. -or- The type of the parent of this method is an interface, and this method is not virtual (Overridable in Visual Basic). |
ArgumentNullException | name is nulla null reference (Nothing in Visual Basic). |
InvalidOperationException | The type was previously created using CreateType. -or- For the current dynamic type, the IsGenericType property is true, but the IsGenericTypeDefinition property is false. |
Remarks
Use this method overload when you do not know the method signature at the time you define the method. For example, the parameter types and return type of a generic method might be specified by the method's generic type parameters, which must be defined after the method has been added to the type. The parameters and return type of the method can be set later using the MethodBuilder.SetSignature method.
This method overload defines a method with CallingConventions.Standard. If you need to define a method without a signature, with a different calling convention, use the DefineMethod(String, MethodAttributes, CallingConventions) method overload.
Examples
The following example defines a generic method named DemoMethod whose parameter type and return type are specified by its generic type parameters. The method is defined without a signature, using the standard calling convention. The MethodBuilder.DefineGenericParameters method is used to make DemoMethod a generic method, and the newly defined type parameters are then used for the signature and return type.
This example is part of a larger example provided for the DefineGenericParameters method.
' Define a Shared, Public method with standard calling
' conventions. Do not specify the parameter types or the
' return type, because type parameters will be used for
' those types, and the type parameters have not been
' defined yet.
Dim demoMethod As MethodBuilder = _
demoType.DefineMethod("DemoMethod", _
MethodAttributes.Public Or MethodAttributes.Static)
...
' Defining generic parameters for the method makes it a
' generic method. By convention, type parameters are
' single alphabetic characters. T and U are used here.
'
Dim typeParamNames() As String = {"T", "U"}
Dim typeParameters() As GenericTypeParameterBuilder = _
demoMethod.DefineGenericParameters(typeParamNames)
' The second type parameter is constrained to be a
' reference type.
typeParameters(1).SetGenericParameterAttributes( _
GenericParameterAttributes.ReferenceTypeConstraint)
...
' Set parameter types for the method. The method takes
' one parameter, and its type is specified by the first
' type parameter, T.
Dim params() As Type = {typeParameters(0)}
demoMethod.SetParameters(params)
' Set the return type for the method. The return type is
' specified by the second type parameter, U.
demoMethod.SetReturnType(typeParameters(1))
// Define a Shared, Public method with standard calling
// conventions. Do not specify the parameter types or the
// return type, because type parameters will be used for
// those types, and the type parameters have not been
// defined yet.
MethodBuilder demoMethod = demoType.DefineMethod(
"DemoMethod",
MethodAttributes.Public | MethodAttributes.Static
);
...
// Defining generic parameters for the method makes it a
// generic method. By convention, type parameters are
// single alphabetic characters. T and U are used here.
//
string[] typeParamNames = { "T", "U" };
GenericTypeParameterBuilder[] typeParameters =
demoMethod.DefineGenericParameters(typeParamNames);
// The second type parameter is constrained to be a
// reference type.
typeParameters[1].SetGenericParameterAttributes(
GenericParameterAttributes.ReferenceTypeConstraint);
...
// Set parameter types for the method. The method takes
// one parameter, and its type is specified by the first
// type parameter, T.
Type[] parms = { typeParameters[0] };
demoMethod.SetParameters(parms);
// Set the return type for the method. The return type is
// specified by the second type parameter, U.
demoMethod.SetReturnType(typeParameters[1]);
Version Information
Silverlight
Supported in: 5, 4, 3
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.