这与这个问题相关:
Retrieving the calling method name from within a method
public Main()
{
PopularMethod();
}
public ButtonClick(object sender, EventArgs e)
{
PopularMethod();
}
public Button2Click(object sender, EventArgs e)
{
PopularMethod();
}
public void PopularMethod()
{
//Get calling method name
}
是否可以使用反射来获取调用其中“PopularMethod”函数的函数列表? 即:[Main,ButtonClick,Button2Click]
更新:C# reflection and finding all references 是我在找什么!活泉!谢谢大家
答案 0 :(得分:2)
您可以使用一个属性让运行时服务为您获取此信息:
public void PopularMethod([System.Runtime.CompilerServices.CallerMemberName] string memberName = "")
{
}
答案 1 :(得分:0)
请查看System.Diagnostics
,其中有StackTrace
个课程,可能会有所帮助。
StackTrace st = new StackTrace(true);
for(int i =0; i< st.FrameCount; i++ )
{
// Note that high up the call stack, there is only
// one stack frame.
StackFrame sf = st.GetFrame(i);
Console.WriteLine();
Console.WriteLine("High up the call stack, Method: {0}",
sf.GetMethod());
Console.WriteLine("High up the call stack, Line Number: {0}",
sf.GetFileLineNumber());
}