首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

获取函数的名称有关问题

2012-02-02 
获取函数的名称问题System.Reflection.MethodInfo.GetCurrentMethod().Name可以获取当前函数的名称函数,

获取函数的名称问题
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分太少了

热点排行