By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,166 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,166 IT Pros & Developers. It's quick & easy.

MethodInfo or MethodBase?

P: n/a
For some time, I've been adding "trace level" logging to my code using

Imports System.Reflection

Private Function XYZ( a1 as A, b1 as B, c1 as C, ...

TRACE.Log( MethodBase.GetCurrentMethod().Name _
, a1, ba, c1 )

but recently, I noticed one of my colleagues using this instead

TRACE.Log( MethodInfo.GetCurrentMethod().Name _
, a1, ba, c1 )

Is there any particular advantage to using either one?

TIA,
Phill W.
Feb 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Phill W. wrote:
For some time, I've been adding "trace level" logging to my code using

Imports System.Reflection

Private Function XYZ( a1 as A, b1 as B, c1 as C, ...

TRACE.Log( MethodBase.GetCurrentMethod().Name _
, a1, ba, c1 )

but recently, I noticed one of my colleagues using this instead

TRACE.Log( MethodInfo.GetCurrentMethod().Name _
, a1, ba, c1 )

Is there any particular advantage to using either one?


Yours is more correct - GetCurrentMethod is a Shared method of
MethodBase, not MethodInfo (which is a subclass of MethodBase).
Although both calls have exactly the same effect, it's in general a bad
idea to invoke Shared functions on subclasses of the class on which
they are actually defined. Indeed, the fact that VB.NET allows this
syntax at all is for many a _bad thing_ (C# by contrast won't let you
do this). To the uninitiated person attempting to look up
MethodInfo.GetCurrentMethod in the help, a nasty surprise awaits.

IMX the most common example you see of this is people doing
Bitmap.FromFile - FromFile is a Shared method on Image, Bitmap's
superclass. This syntax makes the reads think (naturally enough) that
Bitmap.FromFile is going to return a Bitmap - but it doesn't, it
returns an Image. Confusion ensues all round.

VB2005 fixed a similar oddity about Shared members when it made it
warnable to invoke Shared members on instance variables; it's a pity
this construct wasn't similarly dealt with.

--
Larry Lard
Replies to group please

Feb 15 '06 #2

P: n/a
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
TRACE.Log( MethodBase.GetCurrentMethod().Name _
, a1, ba, c1 )
or
TRACE.Log( MethodInfo.GetCurrentMethod().Name _
, a1, ba, c1 )
Yours is more correct
Thank you.
To the uninitiated person attempting to look up
MethodInfo.GetCurrentMethod in the help, a nasty surprise awaits.
Oooh ... I see what you mean ... or rather, I don't! ;-)
VB2005 fixed a similar oddity about Shared members when it made it
warnable to invoke Shared members on instance variables; it's a pity
this construct wasn't similarly dealt with.


Agreed.

Regards,
Phill W.
Feb 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.