vendredi 16 février 2018

C# – Retrieving the original variable/argument name, as it was named, in the caller method – (similar to [CallerMemberName] attribute)

Is there any way to get the original name of the variable/argument, as it was named, in the caller method? In a way, it's like [CallerMemberName] attribute, but for the variable/argument.

Short example:

int ComplexMathMethod(int startPos, int endPos,
    int backgroundStartPos, int backgroundEndStart, 
    int squareStartPos, // …and other positions… )
{
    CheckPosition(startPos);
    CheckPosition(endPos);
    CheckPosition(backgroundStartPos);
    CheckPosition(backgroundEndStart);
    CheckPosition(squareStartPos);
    // …and so on…  
}

void CheckPosition(int position)
{
    bool isValidPosition = // complex check;
    if (!isValidPosition)
    {
        throw new ArgumentException(nameof(position));

        // --->>> It will be extremely convenient to automatically replace "nameof(position)" with original 
        // name of variable/argument from caller method, like: "startPos", "endPos", "backgroundStartPos", etc.

        // Additional string argument with original variable name is not a good solution too.
        // Because, I have similar "check" method that checks 4 connected position’s integers at once.
        // Therefore, 4 input ints with additional +4 string arguments will make a mess.
    }
}


Long example:

void ShowMessage(string messageStr)
{

    // Problem: get original variable/argument name, as it was called, in the caller method.
    string callerMethodArgName;

    // If method was called from "Main" method, than callerMethodArgName should equal to "finishMsg".
    // If method was called from "DoWork" method, than callerMethodArgName should equal to "taskName".
    // If method was called from "StartProgram" method, than callerMethodArgName should equal to "startingMsg".    

    // …missing logic, reflection code, etc.



    // The next line is just a stub.
    callerMethodArgName = nameof(messageStr);



    Console.WriteLine($"Variable name is \"{callerMethodArgName}\"; value is \"{messageStr}\".");

}


void Main()
{

    StartProgram();
    DoWork("Programming…");


    // Example 1.
    string finishMsg = "Finish!";
    ShowMessage(finishMsg);

}

// Example, caller method 2.
void StartProgram()
{
    string startingMsg = "Starting program";
    ShowMessage(startingMsg);
}

// Example, caller method 3.
void DoWork(string taskName)
{
    ShowMessage(taskName);
}

Current program output:

// Variable name is "messageStr"; value is "Starting program".
// Variable name is "messageStr"; value is "Programming…".
// Variable name is "messageStr"; value is "Finish!".

Required/desired output:

// Variable name is "startingMsg"; value is "Starting program".
// Variable name is "taskName"; value is "Programming…".
// Variable name is "finishMsg"; value is "Finish!".


I am not sure if this is possible at all. I spent several hours playing with reflection, but unfortunately, it was fruitless. This would be extremely convenient while debugging in real project.

I will be grateful if someone can give a more accurate headline for this question. Thanks in advance!





Aucun commentaire:

Enregistrer un commentaire