Get caller method

public void SendError(string Message, [CallerMemberName] string callerName = “”)
{
}

using System.Runtime.CompilerServices;

The namespace I am using and the error I get is Screenshot by Lightshot

I read that this is the namespace I need to use for this attritbute. What am I doing wrong?

It’s possible to make this attribute work (just tested). It works in same way as it used to in .NET 4. Attribute is defined in .NET 4.5, but when you define it yourself and compiler supports it, it works.

More information: https://stackoverflow.com/questions/13381917/is-the-callermembername-attribute-in-4-5-able-to-be-faked

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
    public sealed class CallerMemberNameAttribute : Attribute
    {
    }
}

[ExecuteInEditMode]
public class NContainer : MonoBehaviour
{
    void TestLog([System.Runtime.CompilerServices.CallerMemberName] string callerName = "")
    {
        Debug.LogFormat("TestLog called with '{0}'", callerName);
    }

    private void OnEnable()
    {
        TestLog();
    }
}

Output in console:

TestLog called with 'OnEnable'
UnityEngine.Debug:LogFormat(String, Object[])
NContainer:TestLog(String) (at Assets/Scritps/Game/Core/NContainer.cs:58)
NContainer:OnEnable() (at Assets/Scritps/Game/Core/NContainer.cs:63)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

As per this MSDN document on CallerMemberNameAttribute Class it is supported in .NET framework version 4.6, 4.5. So it might not be available in Unity’s mono version.

I even searched it on Unity’s mono compatibility page and couldn’t find it there, so chances are it is not supported.

EDIT:

But this can be achieved using Reflection. Check: Get Calling Method using Reflection [C#]