日期:2014-05-19  浏览次数:20734 次

获取函数的名称问题
System.Reflection.MethodInfo.GetCurrentMethod().Name;
可以获取当前函数的名称函数,我想实现如下功能不知可行不?
void   f1()
{
          this.txt.Text   =   f2();//结果应为:f1
}


string   f2()
{
          //在此写什么样的语句可以返回是那个函数调用了此函数
}


------解决方案--------------------
/// <summary>
/// Gets the name of the function in which this instance of the logger resides.
/// </summary>
/// <returns> The name of the function. </returns>
private static string GetFunctionName()
{
StackTrace st = new StackTrace();
StackFrame sf;
MethodBase function;
string className = string.Empty;
string functionName = string.Empty;
string[] dataParts;
string output = null;

if (st.FrameCount > 0)
{
sf = st.GetFrame(1);
function = sf.GetMethod();
className = function.DeclaringType.ToString();
dataParts = className.Split(new char[] { '. ' });
className = dataParts[dataParts.Length - 1];

functionName = sf.GetMethod().ToString().Trim();
dataParts = functionName.Split(new char[] { ' ', '\t ' }, 2);
functionName = dataParts[1];

output = string.Format( "{0}::{1} ", className, functionName);
}

return output;
}

20分太少了