MethodInfo.GetGenericMethodDefinition Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Overridable Function GetGenericMethodDefinition As MethodInfo
[ComVisibleAttribute(true)]
public virtual MethodInfo GetGenericMethodDefinition()

Return Value

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

Exceptions

Exception Condition
InvalidOperationException

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

NotSupportedException

This method is not supported.

Remarks

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# and Visual Basic 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 

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.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 GetGenericMethodDefinition is present but not supported in Silverlight for Windows Phone.

Examples

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.

' Define a class with a generic method.
Public Class Test
   Public Shared Sub Generic(Of T)(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal toDisplay As T)
      outputBlock.Text &= String.Format(vbCrLf & "Here it is: {0}", toDisplay) & vbCrLf
   End Sub
End Class


...


' Create a Type object representing class Test, and
' get a MethodInfo representing the generic method.
'
Dim ex As Type = GetType(Test)
Dim mi As MethodInfo = ex.GetMethod("Generic")

DisplayGenericMethodInfo(outputBlock, mi)

' Assign the Integer type to the type parameter of the Example 
' method.
'
Dim arguments() As Type = {GetType(Integer)}
Dim miConstructed As MethodInfo = mi.MakeGenericMethod(arguments)

DisplayGenericMethodInfo(outputBlock, miConstructed)


...


' Get the generic type definition from the constructed method,
' and show that it's the same as the original definition.
'
Dim miDef As MethodInfo = miConstructed.GetGenericMethodDefinition()
outputBlock.Text &= String.Format(vbCrLf & "The definition is the same: {0}", _
    miDef Is mi) & vbCrLf
// Define a class with a generic method.
public class Test
{
   public static void Generic<T>(System.Windows.Controls.TextBlock outputBlock, T toDisplay)
   {
      outputBlock.Text += String.Format("\r\nHere it is: {0}", toDisplay) + "\n";
   }
}


...


// Create a Type object representing class Test, and
// get a MethodInfo representing the generic method.
//
Type ex = typeof(Test);
MethodInfo mi = ex.GetMethod("Generic");

DisplayGenericMethodInfo(outputBlock, mi);

// Assign the int type to the type parameter of the Example 
// method.
//
MethodInfo miConstructed = mi.MakeGenericMethod(typeof(int));

DisplayGenericMethodInfo(outputBlock, miConstructed);


...


// Get the generic type definition from the closed method,
// and show it's the same as the original definition.
//
MethodInfo miDef = miConstructed.GetGenericMethodDefinition();
outputBlock.Text += String.Format("\r\nThe definition is the same: {0}",
    miDef == mi) + "\n";

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.