MethodBase.GetCurrentMethod Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a MethodBase
object representing the currently executing method.
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
Returns
GetCurrentMethod() is a static method that is called from within an executing method and that returns information about that method.
A MethodBase
object representing the currently executing method.
Exceptions
This member was invoked with a late-binding mechanism.
Examples
The following example defines two types. The first is a non-generic class, TestClass
, includes a constructor, a method named GetValue
, and a read-write property named GetValue
. The second is a generic class named TestClass<T>
that includes a constructor, a GetValue
method, and a generic method, ConvertValue<Y>
. Each constructor, method, and property accessor includes a call to the GetCurrentMethod method.
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)
Remarks
If the currently executing method is defined on a generic type, the MethodInfo that is returned by GetCurrentMethod is obtained from the generic type definition (that is, MethodBase.ContainsGenericParameters returns true
). Therefore, it does not reflect the type arguments that were used when the method was called. For example, if a method M()
is defined on a generic type C<T>
(C(Of T)
in Visual Basic), and GetCurrentMethod is called from C<string>.M()
, then GetCurrentMethod returns C<T>.M()
(C(Of T).M()
in Visual Basic).
If the currently executing method is a generic method, GetCurrentMethod returns the generic method definition. If the generic method is defined on a generic type, the MethodInfo is obtained from the generic type definition.
Applies to
Pripomienky
https://aka.ms/ContentUserFeedback.
Pripravujeme: V priebehu roka 2024 postupne zrušíme službu Problémy v službe GitHub ako mechanizmus pripomienok týkajúcich sa obsahu a nahradíme ju novým systémom pripomienok. Ďalšie informácie nájdete na stránke:Odoslať a zobraziť pripomienky pre