简体   繁体   English

PowerShell:`New-Object` `-ArgumentList` vs 调用构造函数 vs 类型转换

[英]PowerShell: `New-Object` `-ArgumentList` vs calling constructor vs type casting

I'm just starting to learn some basics of PowerShell and can't get my head around New-Object and type casting.我刚刚开始学习 PowerShell 的一些基础知识,无法理解New-Object和类型转换。 For example this:例如这个:

# all of these yield the same
New-Object System.Net.Sockets.TcpListener -ArgumentList 5000
New-Object System.Net.Sockets.TcpListener(5000)
[System.Net.Sockets.TcpListener]500  # this works

# all of these yield the same
New-Object -TypeName System.Net.Sockets.TCPClient -ArgumentList "8.8.8.8", 53
New-Object System.Net.Sockets.TCPClient("8.8.8.8", 53)
[System.Net.Sockets.TCPClient]::New("8.8.8.8", 53)
# but this doesn't work
# [System.Net.Sockets.TCPClient]("8.8.8.8", 53) # does not work
  • What is the difference between using -ArgumentList and using (arg1,...) ?使用-ArgumentList和使用(arg1,...)什么区别? Is it that the name of the type and the (arg1,...) are interpreted as two different arguments to New-Object even when there is no space between them, and so the (arg1,...) is assigned as the value of -ArgumentList ?是不是类型的名称和(arg1,...)被解释为New-Object两个不同参数,即使它们之间没有空格,因此(arg1,...)被分配为-ArgumentList值?
  • Why doesn't casting work for the second example of System.Net.Sockets.TCPClient ?为什么对System.Net.Sockets.TCPClient的第二个示例进行转换不起作用? The only difference I see is that it takes multiple arguments, whereas System.Net.Sockets.TcpListener takes a single argument.我看到的唯一区别是它需要多个参数,而System.Net.Sockets.TcpListener需要一个参数。 But why can PowerShell cast an integer to a System.Net.Sockets.TcpListener but not an array to a System.Net.Sockets.TCPClient by calling its constructor?但是为什么 PowerShell 可以通过调用其构造函数将整数转换为System.Net.Sockets.TcpListener而不能将数组转换为System.Net.Sockets.TCPClient呢? Ie why aren't these two equivalent:即为什么这两个不等价:
    • [System.Net.Sockets.TCPClient]::New("8.8.8.8", 53) # works
    • [System.Net.Sockets.TCPClient]("8.8.8.8", 53) # does not work

What is the difference between using -ArgumentList and using (arg1, ...) ?使用-ArgumentList和使用(arg1, ...)什么区别?

  • Do not use (arg1,...) it is pseudo method syntax that only happens to work - see the bottom section of this answer .不要使用(arg1,...)它是仅碰巧有效的方法语法 - 请参阅此答案的底部部分。

  • Instead, use arg1, ... - no parentheses, whitespace before the list;相反,使用arg1, ... - 没有括号,列表前有空格; that is the positionally implied equivalent of -ArgumentList arg1, ... (or -Args arg1, ... )这是位置隐含的等价物-ArgumentList arg1, ... (或-Args arg1, ...

Why doesn't casting work for the second example of System.Net.Sockets.TCPClient ?为什么对System.Net.Sockets.TCPClient的第二个示例进行转换不起作用?

Casting only works in two variants:铸造仅适用于两种变体:

  • With a scalar that matches a single-argument constructor or, in the case of a string , if the target type has a static .Parse() method.使用与单参数构造函数匹配的标量,或者在string的情况下,如果目标类型具有静态.Parse()方法。

  • With a hash table ( @{ ... } ) whose entries' keys match properties of the target type and assuming that the target type has a parameterless public constructor.使用哈希表( @{ ... } ),其条目的键匹配目标类型的属性,假设目标类型具有无参数公共构造函数。

Therefore you cannot cast from an array of values.因此,您不能从值数组中进行转换。

See this answer for more information.有关更多信息,请参阅此答案

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

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