none
MethodBase does not contain GetCurrentMethod in Portable Class Library (VS 2015) RRS feed

  • Question

  • Hello,

    I would like to use reflection to print out the name of the method itself defined in a portable class library. Normally, I can get the name of the method by using MethodBase.GetCurrentMethod().Name. But in the Portable Class Library in VS 2015, I got the compiler error saying GetCurrentMethod not defined in MethodBase.

    How do I get around the compiler error?

    The sample/test code attached.

    using System;

    using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Diagnostics;

    namespace ClassLibrary1 {   

    public sealed class Class1    {       

    public delegate int myFunction(object o);       

    public Dictionary<string, myFunction> list = new Dictionary<string, myFunction>();        private static readonly Class1 _instance = new Class1();        static Class1()        {        }        public Class1()        {            string[] n = { "f1", "f2" };            MethodInfo mInfo;            foreach (var i in n)            {                mInfo = typeof(Class1).GetTypeInfo().GetDeclaredMethod(i);                ParameterExpression objExpr = Expression.Parameter(typeof(object));                Expression<myFunction> expr = Expression.Lambda<myFunction>(Expression.Call(null, mInfo, objExpr), objExpr);                list.Add(i, expr.Compile());            }            }        public static Class1 Instance        {            get            {

    return _instance;            }        }        public static int f1(object o)        {            string n = MethodBase.GetCurrentMethod().Name;           

    System.Diagnostics.Debug.WriteLine("current function is {0} ", n);            return 0;        }        public static int f2(object o)        {            string n = MethodBase.GetCurrentMethod().Name;            System.Diagnostics.Debug.WriteLine("current function is {0} ", n);            return 0;        }    }


    Friday, February 26, 2016 10:00 PM

Answers

  • "How do I get around the compiler error?"

    You don't. Either don't use GetCurrentMethod or do not target PCL.

    Why do you need this anyway? Do you have so many methods that simply writing "Debug.WriteLine("current function is f1");" is a real problem?

    If the current method name is enough then you could use CallerMemberNameAttribute to simulate this functionality but that attribute is not available for certain old PCL targets (Silverlight for example).

    Example:

    using System;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    
    public class Program
    {
        static void Main()
        {
            var c = new Program();
            c.expr(42);
        }
    
        Func<object, int> expr;
    
        Program()
        {
            var info = typeof(Program).GetTypeInfo().GetDeclaredMethod("f1");
            var arg = Expression.Parameter(typeof(object));
            expr = Expression.Lambda<Func<object, int>>(Expression.Call(null, info, arg), arg).Compile();
        }
    
        static void LogMethodName([CallerMemberName] string name = null)
        {
            Console.WriteLine("current function is {0} ", name);
        }
    
        public static int f1(object o)
        {
            LogMethodName();
            return 0;
        }
    }
    

    • Marked as answer by FrankTo Saturday, February 27, 2016 1:33 PM
    Saturday, February 27, 2016 7:34 AM
    Moderator

All replies

  • "How do I get around the compiler error?"

    You don't. Either don't use GetCurrentMethod or do not target PCL.

    Why do you need this anyway? Do you have so many methods that simply writing "Debug.WriteLine("current function is f1");" is a real problem?

    If the current method name is enough then you could use CallerMemberNameAttribute to simulate this functionality but that attribute is not available for certain old PCL targets (Silverlight for example).

    Example:

    using System;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    
    public class Program
    {
        static void Main()
        {
            var c = new Program();
            c.expr(42);
        }
    
        Func<object, int> expr;
    
        Program()
        {
            var info = typeof(Program).GetTypeInfo().GetDeclaredMethod("f1");
            var arg = Expression.Parameter(typeof(object));
            expr = Expression.Lambda<Func<object, int>>(Expression.Call(null, info, arg), arg).Compile();
        }
    
        static void LogMethodName([CallerMemberName] string name = null)
        {
            Console.WriteLine("current function is {0} ", name);
        }
    
        public static int f1(object o)
        {
            LogMethodName();
            return 0;
        }
    }
    

    • Marked as answer by FrankTo Saturday, February 27, 2016 1:33 PM
    Saturday, February 27, 2016 7:34 AM
    Moderator
  • Thanks Mike. Yes, I have a couple of hundred functions so manually putting down the name of the function is time consuming just to debug. StackTrace does not exist either, so I am pretty desperate. I'll try LogMethodName and see if it works. If not, I will have to bite the bullet and put in the name of all my functions just to debug.
    Saturday, February 27, 2016 1:38 PM
  • Fair enough, FrankTo.  I was cranky and impatient, and seeing that this functionality was removed due to WinRT (the worst product/design/decision EVER in the history of MSFT client tech) only furthered my raging angst.  WinRT is the reason why .NET was taken from a 10 to a 2. :P  Anyways, I deleted my rude rambling.  Apologies.
    Sunday, May 1, 2016 7:25 PM