怎么样在方法内部自动获取调用此方法的上下文环境?(比如获取:调用此方法的方法的名称,调用此方法的类的名称)如下代码:
public class A
{
public void InvokeA
{
StaticInvoke.StaticInvokeInfo(); //输出字符串 "A","InvokeA"
}
}public class B
{
public void InvokeB
{
StaticInvoke.StaticInvokeInfo(); //输出字符串 "B","InvokeB"
}
}public static class StaticInvoke
{
public static void StaticInvokeInfo()
{
//在此方法内部,自动自发的获取调用者的信息。不用传什么参数进来。
//这种功能,怎么实现呢?
//谁调用了这个方法,就输出谁的信息,可以吗?
}
}//纯讨论,不要说没必要这样做什么的。我就是想知道有没有办法能实现这个功能。//希望我表达够清楚,你们能看懂我在说什么。。 !-_- 谢谢。
public class A
{
public void InvokeA
{
StaticInvoke.StaticInvokeInfo(); //输出字符串 "A","InvokeA"
}
}public class B
{
public void InvokeB
{
StaticInvoke.StaticInvokeInfo(); //输出字符串 "B","InvokeB"
}
}public static class StaticInvoke
{
public static void StaticInvokeInfo()
{
//在此方法内部,自动自发的获取调用者的信息。不用传什么参数进来。
//这种功能,怎么实现呢?
//谁调用了这个方法,就输出谁的信息,可以吗?
}
}//纯讨论,不要说没必要这样做什么的。我就是想知道有没有办法能实现这个功能。//希望我表达够清楚,你们能看懂我在说什么。。 !-_- 谢谢。
public class A
{
public void InvokeA()
{
InvokeA2(); "A","InvokeA","InvokeA2","InvokeA3"
}
public void InvokeA2()
{
InvokeA3(); //输出字符串 "A","InvokeA2","InvokeA3"
}
public void InvokeA3()
{
StaticInvoke.StaticInvokeInfo(); //输出字符串 "A","InvokeA3"
}
} public static class StaticInvoke
{
public static void StaticInvokeInfo()
{
//在此方法内部,自动自发的获取调用者的信息。不用传什么参数进来。
//这种功能,怎么实现呢?
//谁调用了这个方法,就输出谁的信息,追溯到最顶层调用者的信息。可以吗?
}
}
_A.InvokeA();
_A.InvokeA2();
_A.InvokeA3();不知道是不是你想要的 public class A
{
public void InvokeA()
{
InvokeA2(); //"A","InvokeA","InvokeA2","InvokeA3"
}
public void InvokeA2()
{
InvokeA3(); //输出字符串 "A","InvokeA2","InvokeA3"
}
public void InvokeA3()
{
StaticInvoke.StaticInvokeInfo(); //输出字符串 "A","InvokeA3"
}
} public static class StaticInvoke
{
public static void StaticInvokeInfo()
{
System.Diagnostics.StackTrace _StackList = new System.Diagnostics.StackTrace(); System.Diagnostics.StackFrame _StackFrames = _StackList.GetFrame(1); if (_StackFrames == null) return; string _ReflectedType = _StackFrames.GetMethod().ReflectedType.Name;
IList<string> _Method = new List<string>();
_Method.Add(_StackFrames.GetMethod().Name); int _Index = 2;
while (true)
{
_StackFrames = _StackList.GetFrame(_Index); if (_StackFrames == null) break; if (_StackFrames.GetMethod().ReflectedType.Name != _ReflectedType) break; _Method.Add(_StackFrames.GetMethod().Name);
_Index++;
} string _Text = _ReflectedType +" "; for (int i = 0; i != _Method.Count; i++)
{
_Text += _Method[i] + " ";
} System.Windows.Forms.MessageBox.Show(_Text);
}
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
A a = new A();
a.ShowMyInvoke();
a.InvokeA();
}
} public static class InvokeThreadInfo
{
public static void GetInvokeInfo()
{
System.Diagnostics.StackTrace _StackList = new System.Diagnostics.StackTrace(); System.Diagnostics.StackFrame _StackFrames = _StackList.GetFrame(1);
if (_StackFrames == null) return; string _ReflectedType = _StackFrames.GetMethod().ReflectedType.Name;
IList<string> _Method = new List<string>();
_Method.Add(_StackFrames.GetMethod().Name); int _Index = 2;
while (true)
{
_StackFrames = _StackList.GetFrame(_Index); if (_StackFrames == null) break; if (_StackFrames.GetMethod().ReflectedType.Name != _ReflectedType) break; _Method.Add(_StackFrames.GetMethod().Name);
_Index++;
} string _Text = _ReflectedType + " "; for (int i = 0; i != _Method.Count; i++)
{
_Text += _Method[i] + " ";
} System.Windows.Forms.MessageBox.Show(_Text); }
}
public class MyBase
{
public void ShowMyInvoke()
{
System.Diagnostics.StackTrace _StackList = new System.Diagnostics.StackTrace(); System.Diagnostics.StackFrame _StackFrames = _StackList.GetFrame(1); if (_StackFrames == null) return; string _ReflectedType = _StackFrames.GetMethod().ReflectedType.Name;
IList<string> _Method = new List<string>();
_Method.Add(_StackFrames.GetMethod().Name); int _Index = 2;
while (true)
{
_StackFrames = _StackList.GetFrame(_Index); if (_StackFrames == null) break; if (_StackFrames.GetMethod().ReflectedType.Name != _ReflectedType) break; _Method.Add(_StackFrames.GetMethod().Name);
_Index++;
} string _Text = _ReflectedType + " "; for (int i = 0; i != _Method.Count; i++)
{
_Text += _Method[i] + " ";
} System.Windows.Forms.MessageBox.Show(_Text);
}
} public class A:MyBase
{
public void InvokeA()
{
InvokeA2(); //输出字符串 "A","InvokeA","InvokeA2","InvokeA3"
}
public void InvokeA2()
{
InvokeA3(); //输出字符串 "A","InvokeA2","InvokeA3"
}
public void InvokeA3()
{
InvokeThreadInfo.GetInvokeInfo(); //输出字符串 "A","InvokeA3"
}
} public class B:MyBase
{
public void InvokeB()
{
InvokeThreadInfo.GetInvokeInfo(); //输出字符串 "B","InvokeB"
}
}
}如果要在调用 a.InvokeA() 时不但能输出 "A","InvokeA","InvokeA2","InvokeA3", 而且还能输出 "Form1" "button1_Click",即有多少调用方就输出多少信息.