Reflection to get (not the current) method name

Hi, this is my pretend code

public MyClass
{

  public void DoSomething01() { }

  public void DoSomething02()  
{
  if (....)
    throw new Exception("Cannot do this, call DoSomething01 first");
 }
}

Open in new window


When I run my code through the DotNetObsfucator it still reads "Cannot do this, call DoSomething01 first" in the exception thrown at runtime.

My question is how can I read the actual method name replaced from DoSomething01 at runtime using reflection?

Sort of like
    throw new Exception("Cannot do this, call " + getMethodName(MyClass.DoSomething01) + " first");

Open in new window

I can see that System.Reflection.MethodBase.GetCurrentMethod() is a method to get the current method details. I want another method though, not the current one.

Thank you
John BolterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
>>I want another method though, not the current one.

Maybe I am missing something but if you want the name of another method why do you not know the name of the other method you want (or is it just some method at random from the assembly eg. the seventh method of the third class)

In your example you must somehow know that DoSomething01 is to be called before the other method AND that is has not been called.
0
käµfm³d 👽Commented:
To clarify:  You would like your error message to display the obfuscated name of DoSomething01?
0
John BolterAuthor Commented:
Yes
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

John BolterAuthor Commented:
Hello Andy

>>>>I want another method though, not the current one.
>Maybe I am missing something but if you want the name of another
> method why do you not know the name of the other method you want

It is named DoSomething01 in my code.
When I run it through the DotNetObsfucator it is renamed to something else.
What I want is the name of the something else. I also want to be sure when I refactor the method names, from DoSomething01 to XYZ01 that the contents of the string message changes too. The only way I can be sure is to get the method name rather than embedding magic strings for method names that might change in the code.
0
AndyAinscowFreelance programmer / ConsultantCommented:
OK, thanks for explaining just what you need.
0
käµfm³d 👽Commented:
So I'll ask the obvious question:  Obfuscation is about masking your code from someone who might decompile it, as I'm sure you know. But exceptions are meant to be for the developer, as you also probably know. If you obfuscate the method name for the developer, how would he know which method is throwing the exception?
0
John BolterAuthor Commented:
Thanks, I understand, but I still want to know how to do it
0
AndyAinscowFreelance programmer / ConsultantCommented:
Thinking about it and I come to the conclusion I don't know.  I also wonder just how useful is an error message Can't do this, call foobar first to the user.  You might be better to have a generic error message with a specific number code for you to identify where it is.
eg.  Error 49275, Item not initialised before use


ps.  Supply more information in a DEBUG version that is stripped out for the release build
0
käµfm³d 👽Commented:
I haven't worked with any obfuscators yet, so you'll need to test this out. If you make a class-level delegate, then it appears you can get the method name in this fashion:

public class MyClass
{
    private Action obfHelpr;

    public MyClass()
    {
        this.obfHelpr = DoSomething01;
    }

    public void DoSomething01() { }

    public void DoSomething02()
    {
        if (true)
            throw new Exception("Cannot do this, call " + this.obfHelpr.GetMethodInfo().Name  + " first");

    }
}

Open in new window


Screenshot
The downside is that you have to create a delegate per method that you want to mask.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John BolterAuthor Commented:
Wow !!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.