[英]substitute system.reflection for “this”
I have a method I am trying to make a little more easy to widely deploy. 我有一种方法,正在尝试使其更易于广泛部署。
NHibernateISession.log4netLogFileEntry("DEBUG", "hello",
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName);
I would like to reduce the System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName
to a simple this
. 我想将System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName
简化为this
。
But how do I get the FullName
from this.FullName
? 但是如何从this.FullName
获取FullName
?
Just fyi in case it helps you: System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName
gives you the 只是为了防止它对您有所帮助: System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName
为您提供了
<namespace>.<namespace>.<namespace>.<class>
this
is an object - System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
is a type. this
是一个对象System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
是一种类型。 If you want to get the full name of the type of this
then use 如果你想获得的类型的全名this
再使用
this.GetType().FullName
But note that they aren't equivalent. 但是请注意,它们并不等效。 The longer one returns the type that declares the method. 较长的一个返回声明该方法的类型。 If the actual object is a subclass then you'll get the sub-type name. 如果实际对象是子类,则将获得子类型名称。 It also won't work for static
methods, while System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
would. 它也不适用于static
方法,而System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
。
If you actually want the type that declares the method in question then 如果您实际上想要声明相关方法的类型,则
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
is the correct approach. 是正确的方法。 There is no keyword or shortcut that can be used in its place generically. 没有关键字或快捷方式可以在一般位置使用。
System.Exception.StackTrace is an excellent substitute for what I was attempting. System.Exception.StackTrace是我尝试的绝佳替代品。 In fact, it is even better. 实际上,它甚至更好。 And all I have to do is: try { ... } catch (Exception e) { myfunction(e); 我要做的就是:试试{...} catch(Exception e){myfunction(e); } and: MyFunction(Exception e) { log(e.StackTrace) } }和:MyFunction(Exception e){log(e.StackTrace)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.