MethodBase.GetCurrentMethod 메서드

정의

실행 중인 메서드를 나타내는 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

반환

MethodBase

GetCurrentMethod()는 실행 중인 메서드 내에서 호출되고 해당 메서드에 대한 정보를 반환하는 static 메서드입니다.

실행 중인 메서드를 나타내는 MethodBase 개체입니다.

예외

이 멤버가 런타임에 바인딩된 메커니즘을 사용하여 호출되었습니다.

예제

다음 예제에서는 두 가지 형식을 정의합니다. 첫 번째는 제네릭이 아닌 클래스이며, TestClass생성자, 명명된 GetValue메서드 및 이름이 지정된 GetValue읽기-쓰기 속성을 포함합니다. 두 번째는 생성자, 메서드 및 제네릭 메서드ConvertValue<Y>GetValue 포함하는 명명된 TestClass<T> 제네릭 클래스입니다. 각 생성자, 메서드 및 속성 접근자에는 메서드에 대한 호출이 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)에서 가져옵니다. 따라서 메서드가 호출될 때 사용된 형식 인수는 반영되지 않습니다. 예를 들어 메서드 M() 가 제네릭 형식 C<T> (C(Of T)Visual Basic)에서 정의되고 GetCurrentMethod 호출되면 C<string>.M()GetCurrentMethod (Visual Basic)를 반환합니다C<T>.M()``C(Of T).M().

현재 실행 중인 메서드가 제네릭 메서드 GetCurrentMethod 인 경우 제네릭 메서드 정의를 반환합니다. 제네릭 메서드가 제네릭 형식에 정의된 경우 제네릭 형식 MethodInfo 정의에서 가져옵니다.

적용 대상