将 system.reflection 替换为 "this"

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

但请注意,它们并不等同。较长的 returns 声明 方法的类型。如果实际对象是一个子类,那么您将获得子类型名称。它也不适用于 static 方法,而 System.Reflection.MethodBase.GetCurrentMethod().DeclaringType 会。

如果您确实想要声明相关方法的类型,那么

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType

是正确的做法。没有关键字或快捷方式可以通用地代替它。

System.Exception.StackTrace 是我尝试的一个很好的替代品。 事实上,它甚至更好。 而我所要做的就是: try { ... } catch (Exception e) { myfunction(e); } 和: MyFunction(Exception e) { log(e.StackTrace) }