一次设置多个属性 - 相当于 VB 的 With 语句来提供隐含的对象上下文 [英] Setting multiple properties at once - equivalent of VB's With statement to provide an implied object context

查看:63
本文介绍了一次设置多个属性 - 相当于 VB 的 With 语句来提供隐含的对象上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅供学习.

假设我有一个变量,在这个例子中是一个文本框,我如何一次设置多个属性而不需要一遍又一遍地输入变量名.

Let's say I have a variable, in this example a textbox, how could I set many properties at once without typing the variable name over and over.

我自己尝试了一些东西,其中一个因为一些奇怪的原因而起作用.

I've attempted some stuff my self, one of them work for some odd reason.

这个例子有效.Break 用于缩短循环.

This example works. Break is used to shorten the loop.

 $Textbox | % {
    $_.Text = "Hello World"
    $_.Background = "Black"
    $_.Foreground = "Green"
    Break
 }

这两个示例不起作用,在这里只是为了看看我的尝试.

These two examples do not work and are here just to see what I've attempted.

这是带和不带$_.",而不是."我用过="和+=",但都没有用.

This was with and without "$_."s and instead of a "." I've used "=" and "+=", none of which worked.

 $Textbox.@{
    Text = "Hello World"
    Background = "Black"
    Foreground = "Green"
 }

 $Textbox.({
    $_.Text = "Hello World"
    $_.Background = "Black"
    $_.Foreground = "Green"
 })

可能有更简单的方法,我在谷歌上没有遇到任何东西.

There may be a easier way to do, I haven't come across anything on google.

推荐答案

您正在寻找的是一种语言结构,例如 VB[Script] 的 With 语句,它允许您为无对象"设置隐含的上下文;块内的属性引用(例如 .Text).

PowerShell 中没有这样的结构.

There is no such construct in PowerShell.

您的第一次尝试是 PowerShell 中最接近的仿真,尽管以牺牲性能为代价(尽管这可能无关紧要):在管道,自动变量 $_ 允许对手头的输入对象进行简洁的引用.

Your 1st attempt is the closest emulation in PowerShell, albeit at the expense of performance (though that may not matter): in a pipeline, automatic variable $_ allows for a concise reference to the input object at hand.

重要:不要在管道内使用break:它不仅会退出管道,还会退出任何封闭的loop 和,如果没有,封闭脚本.改用return.

Important: Do not use break inside a pipeline: it won't just exit the pipeline, it will exit any enclosing loop and, in the absence of one, the enclosing script. Use return instead.

也就是说,在手头的情况下,只有一个单个输入对象,不需要return.

That said, in the case at hand, with only a single input object, return is not needed.

至于您的其他尝试:

  • 语法 @{...} 仅用于 哈希表文字.尝试将此语法用作属性名称会导致语法错误.

  • Syntax @{...} is only used for hashtable literals. Trying to use this syntax as a property name causes a syntax error.

语法 (...) 计算包含的任何单个命令/表达式;{...} 定义了一个 脚本块.

Syntax (...) evaluates any single command / expression enclosed; {...} defines a script block.

  • 字符串上下文中使用时实际上没有执行的脚本块(需要&)- 例如这里的属性名称,计算其在{}之间的文字内容,即一个多行字符串,显然不是代表$TextBox的现有属性的名称,所以整体结果是$null.

  • A script block that isn't actually executed (which would require &), when used in a string context - such as a property name here, evaluates to its literal contents between { and }, i.e., a multiline string that clearly does not represent the name of an existing property of $TextBox, so the overall result is $null.

请注意,在适当的严格模式下运行的脚本 - 使用 Set-StrictMode -Version 2 或更高 - 会将上述尝试标记为 访问一个不存在的属性作为错误.
相比之下,尝试将赋值给一个不存在的属性总是会产生一个错误.

Note that a script run in the appropriate strict mode - with Set-StrictMode -Version 2 or higher - would have flagged the above attempt to access a non-existent property as an error.
By contrast, an attempt to assign to a non-existent property always generates an error.

但是请注意,PowerShell 在构造对象的上下文中提供了方便的多属性初始化,而在 PowerShell 中,您通常也可以通过强制转换,通过 hashtable 初始化器.

Note, however, that PowerShell offers convenient multi-property initialization in the context of constructing objects, which in PowerShell you can frequently also achieve with casts, via hashtable initializers.

# Cast a hashtable with property name/value pairs to
# [System.Windows.Forms.TextBox], which implicitly constructs
# an instance with the specified property values:
$TextBox = [System.Windows.Forms.TextBox] @{
  Text = "Hello World"
  Location = [Point]::new(10, 50)
}

需要注意的是,此技术的可用性取决于目标类型是否具有无参数构造函数 - 请参阅这个答案 了解详情.

The caveat is that the availability of this technique depends on whether the target type has a parameter-less constructor - see this answer for details.

这篇关于一次设置多个属性 - 相当于 VB 的 With 语句来提供隐含的对象上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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