Class name of Static function

P

Peter Hurford

Hi,

Anybody know if it is possible for a static function to work out, at
runtime, what class it belongs to?

TIA,
Pete
 
P

Patrice

Either using System.Reflection or using a static member ;-)

Also you may want to explain why you need this information in case someone
would have another approach for doing what you want...
 
P

Peter Hurford

Well, the reason is simply to create a generic logging function which
will know (and output) the class/method of its caller to a file or
something.

The method is straightforward, by hooking into the stacktrace.

For non-static classes, getting the classname is even more
straightforward, it is just this.GetType.

However it is not obvious to me how a function in a static class can
work out which class it is in.

Funnily enough the idea of using a static member did cross my mind, but
I really can't be bothered copy/pasting some stupid variable into the
top of all of my static classes unless I really have to. For one,
sooner or later I'll screw it up - believe you me I am *that* bad at
coding; for another there should be a smarter way of doing this.

I am aware that the solution to this problem is likely going to be
something to do with reflection, or maybe using the stack info once
again. But I cannot see how. Any takers?
 
J

Jay R. Wren

Peter said:
Well, the reason is simply to create a generic logging function which
will know (and output) the class/method of its caller to a file or
something.

The method is straightforward, by hooking into the stacktrace.

For non-static classes, getting the classname is even more
straightforward, it is just this.GetType.

However it is not obvious to me how a function in a static class can
work out which class it is in.

Funnily enough the idea of using a static member did cross my mind, but
I really can't be bothered copy/pasting some stupid variable into the
top of all of my static classes unless I really have to. For one,
sooner or later I'll screw it up - believe you me I am *that* bad at
coding; for another there should be a smarter way of doing this.

I am aware that the solution to this problem is likely going to be
something to do with reflection, or maybe using the stack info once
again. But I cannot see how. Any takers?

I think examples of this are found all over the log4net examples page.

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;

May be what you want?

I insert

private static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

at the start of practically all of my classes for use with log4net. I
don't see why this static member should be much different than a static
method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top