MethodBase.GetCurrentMethod 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回代表目前執行方法的 MethodBase
物件。
public:
static System::Reflection::MethodBase ^ GetCurrentMethod();
public static System.Reflection.MethodBase? GetCurrentMethod ();
public static System.Reflection.MethodBase GetCurrentMethod ();
static member GetCurrentMethod : unit -> System.Reflection.MethodBase
Public Shared Function GetCurrentMethod () As MethodBase
傳回
GetCurrentMethod() 是從執行方法中呼叫的靜態方法,並傳回關於該執行方法的資訊。
代表目前執行方法的 MethodBase
物件。
例外狀況
這個成員曾被晚期繫結機制叫用過。
範例
下列範例會定義兩種類型。 第一個是非泛型類別, TestClass
包括建構函式、名為 GetValue
的方法,以及名為的 GetValue
讀寫屬性。 第二個是名為 TestClass<T>
的泛型類別,其中包含建構函式、GetValue
方法和泛型方法。 ConvertValue<Y>
每個建構函式、方法和屬性存取子都包含方法的 GetCurrentMethod 呼叫。
using System;
using System.Reflection;
public class Example
{
public static void Main()
{
var t = new TestClass();
Console.WriteLine(t.GetValue());
t.Value = 10;
Console.WriteLine(t.Value);
Console.WriteLine();
var tg =new Test<int>(200);
Console.WriteLine(tg.GetValue());
var b = tg.ConvertValue<Byte>();
Console.WriteLine("{0} -> {1} ({2})", tg.GetValue().GetType().Name,
b, b.GetType().Name);
}
}
public class TestClass
{
private Nullable<int> _value;
public TestClass()
{
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
}
public TestClass(int value)
{
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
_value = value;
}
public int Value
{
get {
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
return _value.GetValueOrDefault();
}
set {
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
_value = value;
}
}
public int GetValue()
{
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
return this.Value;
}
}
public class Test<T>
{
private T value;
public Test(T value)
{
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
this.value = value;
}
public T GetValue()
{
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
return value;
}
public Y ConvertValue<Y>()
{
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
Console.Write(" Generic method: {0}, definition: {1}, Args: ",
m.IsGenericMethod, m.IsGenericMethodDefinition);
if (m.IsGenericMethod) {
foreach (var arg in m.GetGenericArguments())
Console.Write("{0} ", arg.Name);
}
Console.WriteLine();
try {
return (Y) Convert.ChangeType(value, typeof(Y));
}
catch (OverflowException) {
throw;
}
catch (InvalidCastException) {
throw;
}
}
}
// The example displays the following output:
// Executing TestClass..ctor
// Executing TestClass.GetValue
// Executing TestClass.get_Value
// 0
// Executing TestClass.set_Value
// Executing TestClass.get_Value
// 10
//
// Executing Test`1..ctor
// Executing Test`1.GetValue
// 200
// Executing Test`1.ConvertValue
// Generic method: True, definition: True, Args: Y
// Executing Test`1.GetValue
// Int32 -> 200 (Byte)
Imports System.Reflection
Module Example
Public Sub Main()
Dim t As New TestClass()
Console.WriteLine(t.GetValue())
t.Value = 10
Console.WriteLine(t.Value)
Console.WriteLine()
Dim tg As New Test(Of Integer)(200)
Console.WriteLine(tg.GetValue())
Dim b = tg.ConvertValue(Of Byte)()
Console.WriteLine("{0} -> {1} ({2})", tg.GetValue().GetType().Name,
b, b.GetType().Name)
End Sub
End Module
Public Class TestClass
Private _value As Nullable(Of Integer)
Public Sub New()
Dim m As MethodBase = MethodBase.GetCurrentMethod()
Console.WriteLine(" Executing {0}.{1}",
m.ReflectedType.Name, m.Name)
End Sub
Public Sub New(value As Integer)
Dim m As MethodBase = MethodBase.GetCurrentMethod()
Console.WriteLine(" Executing {0}.{1}",
m.ReflectedType.Name, m.Name)
_value = value
End Sub
Public Property Value As Integer
Get
Dim m As MethodBase = MethodBase.GetCurrentMethod()
Console.WriteLine(" Executing {0}.{1}",
m.ReflectedType.Name, m.Name)
Return _value.GetValueOrDefault()
End Get
Set
Dim m As MethodBase = MethodBase.GetCurrentMethod()
Console.WriteLine(" Executing {0}.{1}",
m.ReflectedType.Name, m.Name)
_value = value
End Set
End Property
Public Function GetValue() As Integer
Dim m As MethodBase = MethodBase.GetCurrentMethod()
Console.WriteLine(" Executing {0}.{1}",
m.ReflectedType.Name, m.Name)
Return Me.Value
End Function
End Class
Public Class Test(Of T)
Private value As T
Public Sub New(value As T)
Dim m As MethodBase = MethodBase.GetCurrentMethod()
Console.WriteLine(" Executing {0}.{1}",
m.ReflectedType.Name, m.Name)
Me.value = value
End Sub
Public Function GetValue() As T
Dim m As MethodBase = MethodBase.GetCurrentMethod()
Console.WriteLine(" Executing {0}.{1}",
m.ReflectedType.Name, m.Name)
Return value
End Function
Public Function ConvertValue(Of Y)() As Y
Dim m As MethodBase = MethodBase.GetCurrentMethod()
Console.WriteLine(" Executing {0}.{1}",
m.ReflectedType.Name, m.Name)
Console.Write(" Generic method: {0}, definition: {1}, Args: ",
m.IsGenericMethod, m.IsGenericMethodDefinition)
If m.IsGenericMethod Then
For Each arg In m.GetGenericArguments()
Console.Write("{0} ", arg.Name)
Next
End If
Console.WriteLine()
Try
Return CType(Convert.ChangeType(value, GetType(Y)), Y)
Catch e As OverflowException
Throw
Catch e As InvalidCastException
Throw
End Try
End Function
End Class
' The example displays the following output:
' Executing TestClass..ctor
' Executing TestClass.GetValue
' Executing TestClass.get_Value
' 0
' Executing TestClass.set_Value
' Executing TestClass.get_Value
' 10
'
' Executing Test`1..ctor
' Executing Test`1.GetValue
' 200
' Executing Test`1.ConvertValue
' Generic method: True, definition: True, Args: Y
' Executing Test`1.GetValue
' Int32 -> 200 (Byte)
備註
如果在泛型型別上定義目前執行的方法, MethodInfo 則會從泛型型別定義 (取得所傳 GetCurrentMethod 回的 ,也就是 MethodBase.ContainsGenericParameters 傳回 true
) 。 因此,它不會反映呼叫 方法時所使用的型別自變數。 例如,如果在 Visual Basic) 中的泛型型別上定義方法 M()
(C(Of T)
,並從 GetCurrentMethod 呼叫 C<string>.M()
,則會GetCurrentMethod在 Visual Basic) 中傳回 C<T>.M()
(C(Of T).M()
。C<T>
如果目前執行中的方法是泛型方法,則 GetCurrentMethod 傳回泛型方法定義。 如果在泛型型別上定義泛型方法,則會 MethodInfo 從泛型型別定義取得 。