Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将system.reflection替换为“反射”;这";_C#_This_System.reflection - Fatal编程技术网

C# 将system.reflection替换为“反射”;这";

C# 将system.reflection替换为“反射”;这";,c#,this,system.reflection,C#,This,System.reflection,我有一个方法,我正在努力使它更易于广泛部署 NHibernateISession.log4netLogFileEntry("DEBUG", "hello", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName); 我想将System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName简化为一个简单的this 但是如何从th

我有一个方法,我正在努力使它更易于广泛部署

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>
。。。

是一个对象-
系统。Reflection.MethodBase.GetCurrentMethod()。DeclaringType
是一个类型。如果要获取此
类型的全名,请使用

this.GetType().FullName
但请注意,它们并不等同。返回声明方法的类型的时间越长。如果实际对象是子类,那么您将获得子类型名称。它也不适用于
static
方法,而
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
则适用

如果您确实想要声明有问题的方法的类型,那么

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType

这是正确的方法。没有任何关键字或快捷方式可以代替它使用。

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

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName
有什么关系?您的意思是
this.GetType().FullName
?转换NHibernateSession.log4netLogFileEntry(“调试”,“您好”,System.Reflection.MethodBase.GetCurrentMethod()).DeclaringType.FullName);-to-NHibernateISession.log4netLogFileEntry(“调试”,“你好”,this)@DStanley这一点都不一样-如果您在派生类中,即使派生类没有重写所讨论的方法,您也会得到派生类的名称。@Luaan我知道,但这似乎是OP所寻找的(对或错)。正确。我知道声明方法的类型。我不想要对象本身。我可能不能用“这个”。但是,是否有一个超级简单的引用,我可以像使用“this”一样通用(如果我只对类的实例(对象)感兴趣)?@user2367083否,没有关键字或快捷方式来定义它。您可以使用
nameof
,但必须插入类和/或方法的名称-无法通用地提取声明类型。被调用的方法是否可以知道/派生调用方法的上下文(没有调用方法发送被调用类的信息)?((我假设没有))是的-并且您已经在使用该方法。我不确定问题是什么,只想减少一行已经相当简单的代码。记住使用(和培训其他人)“this”而不是“blablabla…”