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