5

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
6
  • Did you try get-member -inputobject $a to retrieve members along with their definitions? Commented May 4, 2011 at 11:04
  • @empo: Yes, the create method exist on [system.tuple]::Create with this definition: static System.Tuple[T1,T2] Create[T1, T2](T1 item1, T2 item2) but I'm really stuck hoe use it! Edit: is a static method. Commented May 4, 2011 at 11:17
  • Sorry but I'm not able to use that type at the moment. Anyway I've seen in powershell people which works around tuples. See this and this. Commented May 4, 2011 at 12:14
  • @empo. thanks but my question is how use system.tuple. Yours links talk about a PoSh function to emulate a tuple object. Commented May 4, 2011 at 12:17
  • Yes, that's why it was a comment ;-) Commented May 4, 2011 at 12:22

1 Answer 1

9

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.