Export (0) Print
Expand All

MethodBuilder.DefineGenericParameters Method

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 : 
        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.

ExceptionCondition
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 a null reference (Nothing in Visual Basic).

-or-

An element of names is a null reference (Nothing in Visual Basic).

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 (MSIL Disassembler).

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

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft