2

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.

But how do I get the FullName from this.FullName?

Just fyi in case it helps you: System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName gives you the

<namespace>.<namespace>.<namespace>.<class>
Sinatr
  • 20,892
  • 15
  • 90
  • 319
Steve
  • 905
  • 1
  • 8
  • 32
  • What does `this` have to do with `System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName`? – Luaan Mar 04 '16 at 15:23
  • You mean `this.GetType().FullName`? – D Stanley Mar 04 '16 at 15:24
  • Convert NHibernateISession.log4netLogFileEntry("DEBUG", "hello", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName); - to - NHibernateISession.log4netLogFileEntry("DEBUG", "hello", this); – Steve Mar 04 '16 at 15:25
  • @DStanley That's not the same at all - if you're in a derived class, you'll get the name of the derived class even if it hasn't overriden the method in question. – Luaan Mar 04 '16 at 15:25
  • @Luaan I realize that, but it seems to be what the OP is looking for (right or wrong). – D Stanley Mar 04 '16 at 15:27
  • `Environment.StackTrace` will also give you what you're looking for... – code4life Mar 04 '16 at 19:35

2 Answers2

3

this is an object - System.Reflection.MethodBase.GetCurrentMethod().DeclaringType is a type. If you want to get the full name of the type of this then use

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.

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.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Correct. I what the type that declares the method. I don't want the object itself. I probably can't use 'this'. But is there a super simple reference that I can use as universally as I can use 'this' (if I were only interested in the instance of the class (the object)? – Steve Mar 04 '16 at 15:36
  • 1
    @user2367083 No, there's no keyword or shortcut to define that. You could use `nameof` but you'd have to insert the name of the class and/or method - there's no way to generically extract the declaring type. – D Stanley Mar 04 '16 at 15:41
  • The is there a way that the called method can know/derive the context of the calling method (without the calling method sending the called class it's information)? ((( I'm assuming there isn't ))) – Steve Mar 04 '16 at 16:09
  • 1
    Yes - and you're already using that method. I'm not sure what the problem is other than wanting to reduce an already fairly simple line. – D Stanley Mar 04 '16 at 16:19
  • Well It is a lot easier to remember to use (and to train others) "this" instead of "blablablablablabla..." – Steve Mar 04 '16 at 16:26
  • You can always submit a feature suggestion on http://connect.microsoft.com if you feel like is a feature that is highly valuable. – D Stanley Mar 04 '16 at 20:24
0

System.Exception.StackTrace is an excellent substitute for what I was attempting. In fact, it is even better. And all I have to do is: try { ... } catch (Exception e) { myfunction(e); } and: MyFunction(Exception e) { log(e.StackTrace) }

Steve
  • 905
  • 1
  • 8
  • 32