我正在處理Windows窗體主入口點中的頂級異常。我想訪問在我的處理程序中導致異常的調用方法/程序集。我有一種感覺,我將不得不使用痕跡,但我不知道在哪裏。獲取調用方法導致catch中的異常
Module Program
Sub Main()
Try
AddHandler AppDomain.CurrentDomain.UnhandledException, Function(sender, e) ExceptionHandler.Handle(sender, DirectCast(e.ExceptionObject, Exception))
AddHandler Application.ThreadException, Function(sender, e) ExceptionHandler.Handle(sender, e.Exception)
Application.Run(ApplicationBase)
Catch ex As Exception
MessageBox.Show("Handled Exception")
End Try
End Sub
End Module
Public Class ApplicationBase
Public Sub MethodA()
'Causes an exception
File.ReadAllLines("")
End Sub
End Class
Public Class ExceptionHandler
Public Shared Function Handle(sender As Object, e As Exception)
Dim t As Type = sender.GetType()
'Retrieve the calling method here?
Dim callingMethod = "MethodA"
Return True
End Function
End Class
未來通過作爲發送者的目的是一種螺紋,我試圖看到,如果這將是該調用導致了異常的組件/對象類型。
我的問題是如何獲取方法名稱/信息,並在「handle」方法中推送對象名稱/程序集,如果可能的話?
編輯:
雖然e.ToString()將提出方法的名字 - 我期待訪問一個MethodInfo的列表/程序集/類型到異常是一樣的反射造成的,然後我可以得到.DLL等的版本號 - 我可能在這裏做夢,但是我想知道它是否可能?
編輯2:
我試圖e.TargetSite這爲methodA()異常返回File.ReadAllLines()我正在尋找這將導致異常的類方法的方法信息,因此該方法的信息會方法A - 雖然這比我想象的要接近得多。
哇這一直漫長的一天不知道爲什麼我沒有意識到這一點。但是,這只是一般的異常字符串,我想要methodinfo或單個方法的名稱。反正我有把類型作爲對象或調用程序集,然後我可以用反射/ – LukeHennerley
操縱我相信,e.TargetSite會爲你找到你正在尋找的東西。 –
+1使我意識到 - 但我想要的一個小例子是在處理程序中有一個MethodInfo變量(Dim info爲MethodInfo),然後將其設置爲導致異常(MethodA)的Method。然後,如果可能的話,組件/類型。 – LukeHennerley