Rex_C
Rex_C

Reputation: 2477

Setting a variable to the containing method's name

This should seem pretty simple, but I'm having a time visualizing it. Lets say I have a method:

public Person person(string firstName, string lastName)
{
    //code and such

    string methodName = "Person person";
}

Is there a way to create a variable and set it to the method's name dynamically? I am needing this so when I call a custom exception method, I can set the offending method's name for logging. Any help would be appreciated.

Upvotes: 2

Views: 196

Answers (3)

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

Another solution if using .NET 4.5 is using the CallerMemberName attribute.

public class Example
{
    public void Foo()
    {
        Bar();
    }

    private void Bar([CallerMemberName] string caller = null)
    {
         Console.WriteLine(caller); //Writes "Foo"
    }
}

It does not give you the name of the function you are in but the name of the function who called you. I don't know if this will be useful for your exception handler, but it can be very useful for implementing INotifyPropertyChanged

private string _exampleProperty;

public String ExampleProperty
{
    get { return _exampleProperty; }
    set
    {
        if (value == _exampleProperty) return;
        _exampleProperty = value;
        OnPropertyChanged();
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    var handler = PropertyChanged;
    if (handler != null) 
        handler(this, new PropertyChangedEventArgs(propertyName));
}

Upvotes: 2

John Gibb
John Gibb

Reputation: 10773

You can use this:

System.Reflection.MethodBase.GetCurrentMethod().Name;

If you want the type too, you can use:

var method = System.Reflection.MethodBase.GetCurrentMethod();
var methodName = string.Format("{0} {1}", method.DeclaringType.Name, method.Name);

Edit: Now that I see you're writing a logging function, you may want to try something like this from MSDN docs:

public void DoProcessing()
{
    TraceMessage("Something happened.");
}

public void TraceMessage(string message,
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string sourceFilePath = "",
        [CallerLineNumber] int sourceLineNumber = 0)
{
    Trace.WriteLine("message: " + message);
    Trace.WriteLine("member name: " + memberName);
    Trace.WriteLine("source file path: " + sourceFilePath);
    Trace.WriteLine("source line number: " + sourceLineNumber);
}

The [CallerMemberName] attribute indicates that the compiler should pass in the calling function. This makes it faster than using reflection. Check out this link for more information: http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute(v=vs.110).aspx

Upvotes: 7

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10844

string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;

Upvotes: 4

Related Questions