是否可以打印变量名称? [英] Is it possible to print variable names?

查看:49
本文介绍了是否可以打印变量名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 PowerShell 中打印变量名称?

Is it possible to print variable names in PowerShell?

$myVariable = "My Value"
Write-Host "The variable name is ?????"

预期输出:

变量名是 myVariable

The variable name is myVariable

问题 2(更棘手):是否可以在 PowerShell 中打印传递给函数的变量名称?

Question 2 (more tricky): Is it possible to print variable name passed to a function in PowerShell?

Function MyFunc($myArg){
   Write-Host "The variable name is ?????"
}

$myVariable = "My Value"
MyFunc $myVariable

预期输出:

变量名是 myVariable

The variable name is myVariable

推荐答案

否!

(至少不是以可靠的方式)

原因很简单,当您实际检查函数内部的参数值时,有关作为参数参数引用的变量的上下文信息将被剥离.

The simple reason being that contextual information about a variable being referenced as a parameter argument will have been stripped away by the time you can actually inspect the parameter value inside the function.

在函数被实际调用之前很久,解析器已经评估了每个参数的,并且(可选地)将所述值的类型强制转换为任何类型类型是它绑定到的参数所期望的.

Long before the function is actually called, the parser will have evaluated the value of every single parameter argument, and (optionally) coerced the type of said value to whatever type is expected by the parameter it's bound to.

所以最终作为参数传递给函数的东西不是变量$myVariable,而是(可能被强制的) $myVariable.

So the thing that is ultimately passed as an argument to the function is not the variable $myVariable, but the (potentially coerced) value of $myVariable.

查看about_Parsing帮助文件 (Get-Help about_Parsing) 以获取更多关于此主题的信息

也许还值得注意的是,不能保证参数是变量,而不是文字或其他值表达式:

It's maybe also worth noting that there's no guarantee that a parameter argument is a variable and not a literal or another value expression:

PS C:\> MyFunc 'This is not even a variable'
PS C:\> MyFunc $('Neither','is','this' -join ' ')

<小时>

通过查看调用上下文,您可以获得有关(可能)使用的变量的一些信息,通过 Get-PSCallStack cmdlet:


You can get some information about (potentially) used variables, by looking at the calling context, retrieved through the Get-PSCallStack cmdlet:

function Get-ImmediateInvocationString
{
    param($myArg)

    # Grab second-to-last call
    return @(Get-PSCallStack)[-2].InvocationInfo.Line
}

然后像这样使用它:

PS C:\> $myVariable = "someValue"
PS C:\> Get-ImmediateInvocationString -myArg $myVariable
Get-ImmediateInvocationString -myArg $myVariable

你可以(ab)使用它来推断 -myArg 的参数,比如在调用中抓取最后一个类似变量"的字符串:

You could (ab)use this to infer the argument to -myArg, by say grabbing the last "variable-like" string in the invocation:

function Get-ImmediateInvocationString
{
    param($myArg)

    # Grab second-to-last call
    $Line = @(Get-PSCallStack)[-2].InvocationInfo.Line
    if($Line -match '\$(?<varName>[\w]+)\s*$'){ 
        Write-Host $Matches['varName'] 
    }
}

这似乎在开始时效果很好:

This may seem like it works great at the onset:

PS C:\> Get-ImmediateInvocationString -myArg $myVariable
myVariable

直到出现一个不关心您的一致性期望的用户:

Until a user who doesn't care about your conformance expectations comes along:

PS C:\> Get-ImmediateInvocationString -myArg $myVariable; $lol = ""; Write-Host $lol
lol

所以不,如果不以与实际解析器相同的方式解析调用表达式,你就不能做你想做的事.

So no, you can't do what you want without parsing the calling expression in the same fashion as the actual parser does.

这篇关于是否可以打印变量名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆