如何在powershell中使用system.tuple? [英] How use system.tuple in powershell?

查看:69
本文介绍了如何在powershell中使用system.tuple?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于好奇,这不是我必须拥有它",而是如何在 powershell 中使用 system.tuple 类声明元组?

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

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

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

试试这个:

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...

感谢您的帮助.

如果我尝试:

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

推荐答案

这里有一个方法

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

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

另一个

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

------编辑------

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

回忆

要查看您使用的是哪个 CLR,只需使用 $PSVersionTable

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

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

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 :

powershell.exe.config :

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0.30319"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>

结果:

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

这篇关于如何在powershell中使用system.tuple?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆