MethodBase.GetCurrentMethod 方法

如果当前正在执行的方法定义泛型类型上MethodInfo返回GetCurrentMethod通过泛型类型定义 (即,MethodInfo.ContainsGenericParameters返回true)。 因此,它不反映时调用该方法时所使用的类型自变量。 例如,如果方法M()泛型类型上定义C<T>(C(Of T)在 Visual Basic 中),和GetCurrentMethod从调用C<string>.M(),然后GetCurrentMethod返回C<T>.M()(C(Of T).M()在 Visual Basic 中)。

如果当前正在执行的方法是泛型方法,GetCurrentMethod返回泛型方法定义。 如果在泛型类型上定义的泛型方法MethodInfo从泛型类型定义中获取。

下面的示例定义两种类型。 第一种是一个非泛型类, 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)

 

posted @ 2017-06-09 15:36  herizai  阅读(3183)  评论(0编辑  收藏  举报