[英]substitute system.reflection for “this”
我有一種方法,正在嘗試使其更易於廣泛部署。
NHibernateISession.log4netLogFileEntry("DEBUG", "hello",
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName);
我想將System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName
簡化為this
。
但是如何從this.FullName
獲取FullName
?
只是為了防止它對您有所幫助: System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName
為您提供了
<namespace>.<namespace>.<namespace>.<class>
this
是一個對象System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
是一種類型。 如果你想獲得的類型的全名this
再使用
this.GetType().FullName
但是請注意,它們並不等效。 較長的一個返回聲明該方法的類型。 如果實際對象是子類,則將獲得子類型名稱。 它也不適用於static
方法,而System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
。
如果您實際上想要聲明相關方法的類型,則
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
是正確的方法。 沒有關鍵字或快捷方式可以在一般位置使用。
System.Exception.StackTrace是我嘗試的絕佳替代品。 實際上,它甚至更好。 我要做的就是:試試{...} catch(Exception e){myfunction(e); }和:MyFunction(Exception e){log(e.StackTrace)}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.