在调用方法中检索原始变量/参数名称(类似于[CallerMemberName]属性))

问题描述 投票:1回答:1

有没有办法在调用方法中获取变量/参数的原始名称,因为它已被命名?在某种程度上,它就像[CallerMemberName]属性,但对于变量/参数。

更新2018.02:这个问题类似于以下问题:123,但在2018年初仍然没有有效的标准化解决方案。建议的解决方案:使用Expression<Func<T>>可以拥有non-documented effects并且非常占用CPU,因为转换表达式中的代码只能获取变量的原始名称并不是一个简单的操作。这就像创建一个1М大小的空数组来存储单个整数而不是简单的整数 - 非常无效(«згарматипогоробцям»)。

简短的例子:

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.
    }
}

很长的例子:

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);
}

目前的节目输出:

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

所需/期望的输出:

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

我不确定这是否可行。我花了几个小时玩反射,但不幸的是,它没有结果。在实际项目中进行调试时,这将非常方便。

c# reflection
1个回答
2
投票

你可以使用Linq.Expressions。首先,您需要更改CheckPosition的签名,如下所示:

void CheckPosition(Expression<Func<int>> positionExpression)

之后你可以这样称呼它:

CheckPosition(() => startPos);

完整代码:

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…  

    return 0;
}

void CheckPosition(Expression<Func<int>> positionExpression)
{
    var name = ((MemberExpression) positionExpression.Body).Member.Name;
    var value = positionExpression.Compile().Invoke();

    Console.WriteLine($"Name = {name}, value ={value}");
}
© www.soinside.com 2019 - 2024. All rights reserved.