2013-03-01 55 views
1

在powershell中有一种将散列表转换为System.Version的方法,我得到的错误是无法转换类型为“System.Collections.Hashtable”的“System.Collections.Hashtable”值“键入”System.Version“。将Hashtable转换为System.Version

例 在我的foreach循环,我添加了两个变量键和值dbDictionary

$ dbDictionary.Add($ dbChangesfiles,$线)

然后我想打开将dbDictionary转换为System.Version,这样我就可以将其引用到其他两个已转换为System.Version的变量中。

$ dbDictionaryAsVersion = [System.Version] $ dbDictionary

任何帮助,将不胜感激

回答

1

你不能做到这一点与直接投。你必须要具体,引用包含版本值字典项:

PS> $dbDictionary = @{key1='1.2'; key2='3.4'} 
PS> [System.Version]$dbDictionary['key1'] 

Major Minor Build Revision 
----- ----- ----- -------- 
1  2  -1  -1  

如果该版本包括所有按键:

PS> [System.Version]($dbDictionary.Values -join '.') 

Major Minor Build Revision 
----- ----- ----- -------- 
1  2  3  4  
+0

医生给你开,欢呼声。 – Lewis 2013-03-01 11:46:16

+0

请注意,在hastables中缺少[PSv3中的'[[[ordered]'](http://arcanecode.com/2012/06/04/powershell-v3-ordered-hashtables/))值不能保证顺序你添加它们,所以'@ {A = 1; B = 2}'可以返回'1.2'或'2.1'。 – 2013-03-09 15:55:59

+0

正确,但System.Version的默认格式将覆盖该格式。 – 2013-03-10 12:21:17