Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-06-05
4
Medium Priority
?
1,736 Views
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)
{
    bar(MethodBase.GetCurrentmethod(),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,

marion
0
Comment
Question by:mslawi
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:gbzhhu
ID: 16834035
Copied from http://www.4guysfromviewpoint.com/?m=200501

‘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
0
 

Author Comment

by:mslawi
ID: 16835026
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)
    {
        bar(myMethodInfo,a);
    }
}


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

Author Comment

by:mslawi
ID: 16835092
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...
0
 
LVL 12

Accepted Solution

by:
gbzhhu earned 600 total points
ID: 16841009
I haven't tested.  I am using farework 1.1.  Nice 1 for figuring it out
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Integration Management Part 2
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

580 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