挖土

Coding for fun.
posts - 29, comments - 20, trackbacks - 0, articles - 23
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

2010年7月30日

最近几天一直在学习PowerShell,一直用Notepad在编辑脚本语言,那是一个很痛苦的黑白界面。好在家里可以用Editplus,那还有一点颜色来帮助你。

 

今天终于在Windows PowerShell Blog 上看到一篇博文,PowerShell Integration Into Visual Studio 这让我我很是惊喜,迫不及待的想要安装了。

安装之前,首先要安装

 

PowerGUI VSX is an extension for Visual Studio that adds PowerGUI’s editor with Intellisense, syntax highlighting and snippets for PowerShell script files to Visual Studio!  This can make it much easier to create PowerShell scripts or modules if you’re already working inside Visual Studio.

 

 

 

posted @ 2010-07-30 16:30 挖土. 阅读(33) 评论(0) 编辑

泛型用起来很方便,在PowerShell 2.0的版本中,泛型的定义方法也很简单。下面看看List的使用方法:

1 $foo = New-Object 'System.Collections.Generic.List[int]'
2 $foo.Add(10)
3 $foo.Add(20)
4 $foo.Add(30)
5 $foo
6 
7 #使用Get-Member查看$foo的方法和属性
8 Get-Member -InputObject $foo

  

如果是需要两个参数的Dictionary:

 

 1 $foo = New-Object 'System.Collections.Generic.Dictionary[string,string]'
 2 $foo.Add('FOO','BAR')
 3 $foo.Add('FOB','MENU')
 4 $foo.Add('FOC','MOUSE')
 5 $foo.('FOO')
 6 $foo.Item('FOO')
 7 $foo
 8 
 9 #使用Get-Member查看$foo的方法和属性
10 Get-Member -InputObject $foo

 

 为了方便定义泛型的变量,可以建立一些方法,使用起来就会非常简单。

 

 1 # 创建List变量
 2 Function global:New-GenericList([type] $type)
 3 {
 4     $base = [System.Collections.Generic.List``1]
 5     $qt = $base.MakeGenericType(@($type))
 6     New-Object $qt
 7 }
 8 
 9 # 创建Dictionary变量
10 Function global:New-GenericDictionary([type] $keyType, [type] $valueType)
11 {
12     $base = [System.Collections.Generic.Dictionary``2]
13     $qc = $base.MakeGenericType(($keyType$valueType))
14     New-Object $qc
15 }

 

这样子使用起来就非常简单了:

 

 1 PS D:\> $intList = New-GenericList int
 2 PS D:\> $intList.Add(10)
 3 PS D:\> $intList.Add(20)
 4 PS D:\> $intList.Add(30)
 5 PS D:\> $intList
 6 10
 7 20
 8 30
 9 
10 PS D:\> $gd = New-GenericDictionary string int
11 PS D:\> $gd["Red"= 1
12 PS D:\> $gd["Blue"= 2
13 PS D:\> $gd
14 
15 Key          Value
16 ---          -----
17 Red              1
18 Blue             2

posted @ 2010-07-30 11:27 挖土. 阅读(81) 评论(0) 编辑