How to get a MethodBase for a method other than the current method

Posted on 2006-06-05
Last Modified: 2010-08-05
MethodBase.GetCurrentMethod() returns a MethodBase object for the current method. I need a way to return a MethodBase for a method other than the current method. I am doing this from a method which is an overridden version of the method of interest

GetMethodFromHandle() seems like it might work, but is it available in .NET 1.1? I think it may only be available in 2.0. Also it takes a MethodHandle - how do I get a MethodHandle for a given method?

Here is an example:

void foo(a)
void foo(a,b)
    if (b==ACertainValue)
        bar(--->MethodBase of foo(a), not foo(a,b)<---,a);

So, I want to call bar with the MethodBase of foo(a) in both functions. If I use GetCurrentMethod() in foo(a,b), it returns the MethodBase of foo(a,b), but what I want is the MethodBase of foo(a). (i.e. bar should not notice anything different between being called by foo(a) or foo(a,b)

Thanks in Advance,

Question by:mslawi
    LVL 12

    Expert Comment

    Copied from

    ‘Returns a methods name, based on the current stacktrace
    Friend Shared Function ReturnSource() As String
           Dim aStack As New System.Diagnostics.StackTrace
           Dim aMethod As System.Reflection.MethodBase
           ‘Use Frame 1, as Frame 0 is this Method
           aMethod = System.Reflection.MethodInfo.GetMethodFromHandle(aStack.GetFrame(1).GetMethod.MethodHandle)
           Return aMethod.ReflectedType.FullName + “.” + aMethod.Name
    End Function

    Author Comment

    I eventually found the answer:
    MethodInfo is inherited from MethodBase and can be substituted. This works for .NET 1.1 without using GetMethodFromHandle().

    void foo(a,b)
        Type myType = typeof(thisClass);
        MethodInfo myMethodInfo = myType.GetMethod("foo",
                                       new Type[] {typeof(a)});
        if (b==ACertainValue)

                      Type myType = typeof(FrontEnd);
                      MethodInfo myMethodInfo = myType.GetMethod("SetSourceControlRegs",
                                                                   new Type[] {typeof(ushort[])});

    Author Comment

    Thanks, gbzhhu, for showing how to get a handle. So does using Frame 1 reflect to foo(a)?

    Also ignore the code after function foo() in my comment above - I meant to delete that...
    LVL 12

    Accepted Solution

    I haven't tested.  I am using farework 1.1.  Nice 1 for figuring it out

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now