MethodInfo.GetGenericMethodDefinition Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Olyan objektumot MethodInfo ad vissza, amely egy általános metódusdefiníciót jelöl, amelyből az aktuális metódus felépíthető.
public:
virtual System::Reflection::MethodInfo ^ GetGenericMethodDefinition();
public virtual System.Reflection.MethodInfo GetGenericMethodDefinition();
[System.Runtime.InteropServices.ComVisible(true)]
public virtual System.Reflection.MethodInfo GetGenericMethodDefinition();
abstract member GetGenericMethodDefinition : unit -> System.Reflection.MethodInfo
override this.GetGenericMethodDefinition : unit -> System.Reflection.MethodInfo
[<System.Runtime.InteropServices.ComVisible(true)>]
abstract member GetGenericMethodDefinition : unit -> System.Reflection.MethodInfo
override this.GetGenericMethodDefinition : unit -> System.Reflection.MethodInfo
Public Overridable Function GetGenericMethodDefinition () As MethodInfo
Válaszok
Egy MethodInfo általános metódusdefiníciót képviselő objektum, amelyből az aktuális metódus felépíthető.
- Attribútumok
Kivételek
Az aktuális metódus nem általános módszer. Vagyis IsGenericMethod visszaadja false.
Ez a módszer nem támogatott.
Példák
Az alábbi példakód egy általános metódussal rendelkező osztályt és a metódus beszerzéséhez MethodInfo szükséges kódot mutatja be, a metódust argumentumok beírásához köti, és visszaszerzi az eredeti általános típusdefiníciót a kötött metódusból.
Ez a példa a metódushoz MakeGenericMethod megadott nagyobb példa része.
// Define a class with a generic method.
public class Example
{
public static void Generic<T>(T toDisplay)
{
Console.WriteLine("\r\nHere it is: {0}", toDisplay);
}
}
' Define a class with a generic method.
Public Class Example
Public Shared Sub Generic(Of T)(ByVal toDisplay As T)
Console.WriteLine(vbCrLf & "Here it is: {0}", toDisplay)
End Sub
End Class
// Create a Type object representing class Example, and
// get a MethodInfo representing the generic method.
//
Type ex = typeof(Example);
MethodInfo mi = ex.GetMethod("Generic");
DisplayGenericMethodInfo(mi);
// Assign the int type to the type parameter of the Example
// method.
//
MethodInfo miConstructed = mi.MakeGenericMethod(typeof(int));
DisplayGenericMethodInfo(miConstructed);
' Create a Type object representing class Example, and
' get a MethodInfo representing the generic method.
'
Dim ex As Type = GetType(Example)
Dim mi As MethodInfo = ex.GetMethod("Generic")
DisplayGenericMethodInfo(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(miConstructed)
// Get the generic type definition from the closed method,
// and show it's the same as the original definition.
//
MethodInfo miDef = miConstructed.GetGenericMethodDefinition();
Console.WriteLine("\r\nThe definition is the same: {0}",
miDef == mi);
' 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()
Console.WriteLine(vbCrLf & "The definition is the same: {0}", _
miDef Is mi)
Megjegyzések
Az általános metódusdefiníciók olyan sablonok, amelyekből metódusok hozhatók létre. Az általános metódusdefiníciós T M<T>(T t) (C# szintaxisban kifejezve, Function M(Of T)(ByVal tVal As T) As T Visual Basic) például létrehozhatja és meghívhatja a metódust int M<int>(int t) (Function M(Of Integer)(ByVal tVal As Integer) As Integer Visual Basic). A létrehozott metódust MethodInfo képviselő objektum alapján a GetGenericMethodDefinition metódus az általános metódusdefiníciót adja vissza.
Ha két létrehozott metódus ugyanabból az általános metódusdefinícióból jön létre, a GetGenericMethodDefinition metódus ugyanazt MethodInfo az objektumot adja vissza mindkét metódushoz.
Ha olyan metódust GetGenericMethodDefinition hív megMethodInfo, amely már egy általános metódusdefiníciót jelöl, az az aktuális MethodInfoértéket adja vissza.
Ha egy általános metódusdefiníció a deklarálási típus általános paramétereit tartalmazza, minden egyes létrehozott típusra vonatkozóan lesz egy általános metódusdefiníció. Vegyük például a következő kódot:
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
A C<int> (C(Of Integer) Visual Basic) típusban az általános M metódus B<int, S> ad vissza. A nyitott típusban C<T>a M visszaadott értéket adja B<T, S>vissza. Mindkét esetben a tulajdonság visszaadja IsGenericMethodDefinitiontrue az MethodInfo adott értéket M, így MakeGenericMethod mindkét MethodInfo objektumon meghívható. A létrehozott típus esetében a hívás MakeGenericMethodMethodInfo eredménye meghívható. Nyitott típus MethodInfo esetén a visszaadott MakeGenericMethod érték nem hívható meg.
Az általános metódusokra vonatkozó kifejezések invariáns feltételeinek listáját a IsGenericMethod tulajdonságban találja. Az általános tükrözésben használt egyéb kifejezések invariáns feltételeinek listáját a IsGenericType tulajdonságban találja.