dotnet通过反射获得呼叫者

时间:2014-10-13 15:49:10

标签: c# reflection stack-trace

在form1的load事件中有这个代码,为什么条件总是假,即使调用者是form1?

如何让调用者的实例读取属性?

private void Form1_Load(object sender, EventArgs e)
{
    Utility.DoSomething();
}
//in a static class called utility
public static void DoSomething()
{
    try
    {
        throw new NotImplementedException();
    }
    catch (Exception ex)
    {
        ManageException(ex);
    }
}

//in a static class called utility
public static void ManageException(Exception ex)
{
 StackTrace st = new System.Diagnostics.StackTrace();
  for (int i = 0; i < st.FrameCount; i++)
    if (st.GetFrame(i).GetMethod().DeclaringType is System.Windows.Forms.Form)
     Console.Write("i need to get the form instance to access the name property");
}

0 个答案:

没有答案