如何在powershell中使用system.tuple?

CB.*_*CB. 5 powershell tuples

只是出于好奇,它不是'我必须拥有它',而是如何使用powershell中的system.tuple类声明一个元组?

我正在使用powershell.exe.config加载框架4.0,但我无法创建元组.

试试这个:

PS C:\ps1> $a = [System.Tuple``2]::Create( "pino", 34)

Chiamata al metodo non riuscita. [System.Tuple`2] non contiene un metodo denominato 'Create'.
In riga:1 car:31
+ $a = [System.Tuple``2]::Create <<<< ( "pino", 34)
    + CategoryInfo          : InvalidOperation: (Create:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
Run Code Online (Sandbox Code Playgroud)

抱歉意大利样品......

谢谢你的帮助.

编辑:

如果我尝试:

PS C:\ps1> $a = [System.Tuple]::Create(34,"pino")

Impossibile trovare un overload per "Create" e il numero di argomenti: "2".
In riga:1 car:28
+ $a = [System.Tuple]::Create <<<< (34,"pino")
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
Run Code Online (Sandbox Code Playgroud)

JPB*_*anc 9

这是一种方式

PS> $a = New-Object 'Tuple[string,int]'("Jack", 78)
PS> $a

Item1                                             Item2
-----                                             -----
Jack                                              78
Run Code Online (Sandbox Code Playgroud)

另一个

PS> $dpt = New-Object 'Tuple[string,string,int]'("Cantal", "Aurillac", 15)
PS> $dpt.Item2
Aurillac
Run Code Online (Sandbox Code Playgroud)

- - - 编辑 - - -

召回

要查看您正在使用的CLR,请使用 $PSVersionTable

PS C:\> $PSVersionTable
Name                           Value
----                           -----
CLRVersion                     2.0.50727.4959
BuildVersion                   6.1.7600.16385
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1
Run Code Online (Sandbox Code Playgroud)

如果您希望PowerShell开始使用CLR 4.0,您必须将该文件放在该文件powershell.exe.config夹中$PSHOME(C:\ Windows\System32\WindowsPowerShell\v1.0)

powershell.exe.config:

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0.30319"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>
Run Code Online (Sandbox Code Playgroud)

结果:

PS C:\Users\JPB> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      2.0
PSCompatibleVersions           {1.0, 2.0}
BuildVersion                   6.1.7600.16385
PSRemotingProtocolVersion      2.1
WSManStackVersion              2.0
CLRVersion                     4.0.30319.225
SerializationVersion           1.1.0.1
Run Code Online (Sandbox Code Playgroud)