MethodBuilder.DefineGenericParameters Method (String[])

 

Sets the number of generic type parameters for the current method, specifies their names, and returns an array of GenericTypeParameterBuilder objects that can be used to define their constraints.

Namespace:   System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

member DefineGenericParameters : 
        [<ParamArrayAttribute>] names:string[] -> GenericTypeParameterBuilder[]

Parameters

names
Type: System.String[]

An array of strings that represent the names of the generic type parameters.

Return Value

Type: System.Reflection.Emit.GenericTypeParameterBuilder[]

An array of GenericTypeParameterBuilder objects representing the type parameters of the generic method.

Exception Condition
InvalidOperationException

Generic type parameters have already been defined for this method.

-or-

The method has been completed already.

-or-

The SetImplementationFlags method has been called for the current method.

ArgumentNullException

names is null.

-or-

An element of names is null.

ArgumentException

names is an empty array.

Calling the DefineGenericParameters method makes the current method generic. There is no way to undo this change. Calling this method a second time causes an InvalidOperationException.

The type parameters of the generic method can be retrieved later by using the GetGenericArguments method.

By convention, a type parameter name is a single uppercase letter.

For more information, see MethodInfo.IsGenericMethod and MethodInfo.GetGenericMethodDefinition. For information on generic types, see Type.IsGenericType.

The following code example creates a dynamic type, DemoType, which contains the dynamic generic method DemoMethod. This method has two generic type parameters, one of which is used as a parameter, and the other as the return type.

When the code is executed, the dynamic assembly is saved as DemoGenericMethod1.dll, and can be examined using the Ildasm.exe (IL Disassembler).

System_CAPS_noteNote

This code example generates a simple method body that merely returns a null reference. For a code example with a more fully developed method body that creates and uses generic types, see How to: Define a Generic Method with Reflection Emit.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Silverlight
Available since 2.0
Return to top
Show: