Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2006

Tip: Looking for answers? Try searching our database.

Class name of Static function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Hurford - 30 Jan 2006 17:53 GMT
Hi,

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

TIA,
Pete
Patrice - 30 Jan 2006 18:51 GMT
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...

Signature

Patrice

> Hi,
>
[quoted text clipped - 3 lines]
> TIA,
> Pete
Peter Hurford - 31 Jan 2006 10:26 GMT
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?
Jay R. Wren - 31 Jan 2006 18:06 GMT
> 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
[quoted text clipped - 17 lines]
> 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.

--
Jay R. Wren

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.