Export (0) Print
Expand All

MethodInfo.GetGenericMethodDefinition Method

Returns a MethodInfo object that represents a generic method definition from which the current method can be constructed.

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

[<ComVisibleAttribute(true)>]
abstract GetGenericMethodDefinition : unit -> MethodInfo 
[<ComVisibleAttribute(true)>]
override GetGenericMethodDefinition : unit -> MethodInfo 

Return Value

Type: System.Reflection.MethodInfo
A MethodInfo object representing a generic method definition from which the current method can be constructed.

ExceptionCondition
InvalidOperationException

The current method is not a generic method. That is, IsGenericMethod returns false.

NotSupportedException

This method is not supported.

A generic method definition is a template from which methods can be constructed. For example, from the generic method definition T M<T>(T t) (expressed in C# syntax; Function M(Of T)(ByVal tVal As T) As T in Visual Basic) you can construct and invoke the method int M<int>(int t) (Function M(Of Integer)(ByVal tVal As Integer) As Integer in Visual Basic). Given a MethodInfo object representing this constructed method, the GetGenericMethodDefinition method returns the generic method definition.

If two constructed methods are created from the same generic method definition, the GetGenericMethodDefinition method returns the same MethodInfo object for both methods.

If you call GetGenericMethodDefinition on a MethodInfo that already represents a generic method definition, it returns the current MethodInfo.

If a generic method definition includes generic parameters of the declaring type, there will be a generic method definition specific to each constructed type. For example, consider the following C#, Visual Basic, and C++ code:

class B<U,V> {}
class C<T> { public B<T,S> M<S>() {...}}

Class B(Of U, V)
End Class
Class C(Of T)
    Public Function M(Of S)() As B(Of T, S)
        ...
    End Function
End Class 

generic <typename U, typename V> ref class B {};
generic <typename T> ref class C
{
public:
    generic <typename S> B<T,S>^ M() {...};
};

In the constructed type C<int> (C(Of Integer) in Visual Basic), the generic method M returns B<int, S>. In the open type C<T>, M returns B<T, S>. In both cases, the IsGenericMethodDefinition property returns true for the MethodInfo that represents M, so MakeGenericMethod can be called on both MethodInfo objects. In the case of the constructed type, the result of calling MakeGenericMethod is a MethodInfo that can be invoked. In the case of the open type, the MethodInfo returned by MakeGenericMethod cannot be invoked.

For a list of the invariant conditions for terms specific to generic methods, see the IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the IsGenericType property.

The following code example shows a class with a generic method and the code required to obtain a MethodInfo for the method, bind the method to type arguments, and get the original generic type definition back from the bound method.

This example is part of a larger example provided for the MakeGenericMethod method.

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

Portable Class Library

Supported in: Portable Class Library

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