Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use system.tuple in powershell?

Just for curiosity, it's not a 'I must have it', but how declare a tuple using system.tuple class in powershell?

I'm using powershell.exe.config to load framework 4.0 but I'm not able to create a tuple.

Trying this:

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

sorry for Italian sample...

Thank you for help.

EDIT:

if i try:

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
like image 880
CB. Avatar asked May 04 '11 10:05

CB.


1 Answers

Here is a way

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

Item1                                             Item2
-----                                             -----
Jack                                              78

Another one

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

------EDIT------

Recall

to see which CLR you are using, just use $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

if you want PowerShell to start using CLR 4.0 you have to put the file powershell.exe.config in the folder $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>

Result :

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
like image 50
JPBlanc Avatar answered Nov 15 '22 05:11

JPBlanc