Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用反射打印自己的方法名、类库(可移植)_C#_Reflection_Visual Studio 2015 - Fatal编程技术网

C# 使用反射打印自己的方法名、类库(可移植)

C# 使用反射打印自己的方法名、类库(可移植),c#,reflection,visual-studio-2015,C#,Reflection,Visual Studio 2015,我想使用反射作为方法来打印自己的名称。我在不同的线程上发现了类似的问题,但他们的方法似乎不适用于我的场景。建议使用 MethodBase.GetCurrentMethod().Name 我得到的编译器错误是“MethodBase”不包含“GetCurrentMethod”的定义 获取当前方法自己的名称,但GetCurrentMethod在VS2015的可移植类库中不存在。以下是我的代码供您参考。请注意:VS版本中一些早于2015年的可移植类库可能会工作,我有vs2015,并得到了错误 nam

我想使用反射作为方法来打印自己的名称。我在不同的线程上发现了类似的问题,但他们的方法似乎不适用于我的场景。建议使用

MethodBase.GetCurrentMethod().Name 
我得到的编译器错误是“MethodBase”不包含“GetCurrentMethod”的定义

获取当前方法自己的名称,但GetCurrentMethod在VS2015的可移植类库中不存在。以下是我的代码供您参考。请注意:VS版本中一些早于2015年的可移植类库可能会工作,我有vs2015,并得到了错误

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());
            }

            list.Add("f11", f1);
            list.Add("f21", f2);
        }
        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;
        }
    }

}
命名空间类库1
{
公共密封类别1
{
公共委托int myFunction(对象o);
公共字典列表=新字典();
私有静态只读类1_实例=新类1();
静态类别1()
{
}
公共类别1()
{
字符串[]n={“f1”,“f2”};
MethodInfo-mInfo;
foreach(n中的变量i)
{
mInfo=typeof(Class1).GetTypeInfo().GetDeclaredMethod(i);
ParameterExpression objExpr=Expression.Parameter(typeof(object));
Expression expr=Expression.Lambda(Expression.Call(null、mInfo、objExpr)、objExpr);
Add(i,expr.Compile());
}
列表。添加(“f11”,f1);
列表。添加(“f21”,f2);
}
公共静态类1实例
{
得到
{
返回_实例;
}
}
公共静态int f1(对象o)
{
字符串n=MethodBase.GetCurrentMethod().Name;
System.Diagnostics.Debug.WriteLine(“当前函数为{0}”,n);
返回0;
}
公共静态int f2(对象o)
{
字符串n=MethodBase.GetCurrentMethod().Name;
System.Diagnostics.Debug.WriteLine(“当前函数为{0}”,n);
返回0;
}
}
}

有什么想法吗?

您也可以从
StackTrace

StackTrace st = new StackTrace();
var functionName = st.GetFrame(0).GetMethod().Name;

Namespace:   System.Diagnostics
Assembly:  mscorlib (in mscorlib.dll)

StackTrace似乎也不存在于可移植类库中。是否检查了命名空间?是的,它不存在。