Returns a System.Reflection.MethodInfo object that represents a generic method definition from which the current method can be constructed.
A System.Reflection.MethodInfo object representing a generic method definition from which the current method can be constructed.
Type Reason InvalidOperationException The current method is not a generic method. That is, MethodInfo.IsGenericMethod returns false.
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 System.Reflection.MethodInfo object representing this constructed method, the MethodInfo.GetGenericMethodDefinition method returns the generic method definition.
If two constructed methods are created from the same generic method definition, the MethodInfo.GetGenericMethodDefinition method returns the same System.Reflection.MethodInfo object for both methods.
If you call MethodInfo.GetGenericMethodDefinition on a System.Reflection.MethodInfo that already represents a generic method definition, it returns the current System.Reflection.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:
Example
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 MethodInfo.IsGenericMethodDefinition property returns true for the System.Reflection.MethodInfo that represents M, so MethodInfo.MakeGenericMethod(Type[]) can be called on both System.Reflection.MethodInfo objects. In the case of the constructed type, the result of calling MethodInfo.MakeGenericMethod(Type[]) is a System.Reflection.MethodInfo that can be invoked. In the case of the open type, the System.Reflection.MethodInfo returned by MethodInfo.MakeGenericMethod(Type[]) cannot be invoked.
For a list of the invariant conditions for terms specific to generic methods, see the MethodInfo.IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the Type.IsGenericType property.
The following code shows a class with a generic method and the code required to obtain a System.Reflection.MethodInfo for the method, bind the method to type arguments, and get the original generic type definition back from the bound method. (It is part of a larger example for the method MethodInfo.MakeGenericMethod.)
C# Example
// Define a class with a generic method. public class Example { public static void Generic<T>(T toDisplay) { Console.WriteLine("\nHere it is: {0}", toDisplay); } } // ... // Create a Type object representing class Example, and // get a MethodInfo representing the generic method. // Type ex = Type.GetType("Example"); MethodInfo mi = ex.GetMethod("Generic"); DisplayGenericMethodInfo(mi); // Bind the type parameter of the Example method to // type int. // Type[] arguments = {typeof(int)}; MethodInfo miBound = mi.MakeGenericMethod(arguments); DisplayGenericMethodInfo(miBound); // ... // Get the generic type definition from the closed method, // and show it's the same as the original definition. // MethodInfo miDef = miBound.GetGenericMethodDefinition(); Console.WriteLine("\nThe definition is the same: {0}", miDef == mi);