简体   繁体   English

关于变量初始化和传递 arguments 的基本 Windows 10 Powershell 问题

[英]Basic Windows 10 Powershell question on variable initializaton and passing arguments

I am currently learning about Powershell and how to write a script to launch an application.我目前正在学习 Powershell 以及如何编写脚本来启动应用程序。 The following snippet of code I borrowed and have modified to learn how to launch notepad.我借用并修改了以下代码片段以了解如何启动记事本。 The question I have is what does $args.Clone() do or derive from?我的问题是$args.Clone()做什么或源自什么? (It is from the original code which had a different path and executable program being defined/called.) I realize that the variable $myArgs is being initialized to the left of the equal sign by the function on the right. (它来自具有不同路径和被定义/调用的可执行程序的原始代码。)我意识到变量 $myArgs 正在由右侧的 function 初始化到等号的左侧。 However, I have not been successful finding resources about what can you can do with .Clone() so I thought I would try and ask here.但是,我没有成功找到有关.Clone()可以做什么的资源,所以我想我会在这里尝试询问。

BTW, the script works as it launchs notepad.exe and names the text file 'pp'.顺便说一句,该脚本在启动 notepad.exe 并将文本文件命名为“pp”时工作。 If the file has not previously been created, it asks me if I want to name the text file 'pp'.如果该文件之前没有创建,它会询问我是否要将文本文件命名为“pp”。

$exePath = $env:NOTEPAD_HOME + '/Windows/notepad.exe'
$myArgs = $args.Clone()
$myArgs += '-pp'
$myArgs += $env:NOTEPAD_HOME
& $exePath $myArgs

tl;dr tl;博士

Your modification of $myArgs can be simplified as follows:您对$myArgs的修改可以简化如下:

$myArgs = $args + '-pp' + $env:NOTEPAD_HOME

With an array as the LHS, operator + performs concatenation , ie, it appends to the array, though note that appending means that a new array is created in the process, given that arrays are fixed-size data structures in .NET.使用数组作为 LHS,运算符+执行连接,即它附加到数组,但请注意,附加意味着在此过程中创建一个数组,因为 arrays 是 .NET 中的固定大小数据结构。

The fact that a new array is implicitly created anyway makes the call to .Clone() in your code unnecessary.无论如何都会隐式创建一个数组这一事实使得在您的代码中调用.Clone()是不必要的。


Background Information背景资料

Typically - and in the case of the automatic $args variable - the .Clone() method is an implementation of the System.ICloneable interface .通常——在自动$args变量的情况下——. .Clone()方法是System.ICloneable接口的实现。

The purpose of .Clone() is to create a copy (clone) of an object, but - as the answer linked to by BACON explains - the semantics of this operation - shallow (see below) vs. deep copying - aren't prescribed, which is why use of System.ICloneable is generally discouraged . .Clone()的目的是创建 object 的副本(克隆),但是 - 正如BACON链接的答案所解释的那样 - 此操作的语义-(见下文)与复制 - 没有规定,这就是为什么通常不鼓励使用System.ICloneable的原因。

$args is an array ( System.Array ), and for arrays, .Clone() creates a so-called shallow copy. $args是一个数组( System.Array ),对于 arrays, .Clone()创建一个所谓的拷贝。 That is, how the elements of the array are copied depends on whether they are instances of value types or reference types:也就是说,数组元素的复制方式取决于它们是值类型的实例还是引用类型:

  • for value-type elements, the new array will have an independent copy of the data of the original array's elements.对于值类型元素,新数组将拥有原始数组元素数据的独立副本

  • for reference-type elements, the new array will have a copy of the reference of the original array's elements, which means that elements of both arrays point to same data (objects).对于引用类型元素,新数组将具有原始数组元素引用的副本,这意味着 arrays 的元素都指向相同的数据(对象)。

For more information about value types vs. reference types, see this answer .有关值类型与引用类型的更多信息,请参阅此答案

PowerShell implicitly clones arrays in the way described when you use += and + . PowerShell 以使用+=+时描述的方式隐式克隆 arrays

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM