2016-12-20 18 views
0

私はゆっくりPowerShellを教えています。私はenumsに完全に戸惑います。私の理解では、それらは本当に整数値であるかのフレンドリーな名前の集合です。さて、それは素晴らしいです...しかし、PowerShellに実際にそれらを見させるにはどうしたらいいですか?PowerShellに.NETのenum値を "参照"させるにはどうすればよいですか?

例:PowerShellは "PictrueBoxSizeMode.StretchImage" は何であったかの任意の手掛かりを持っていた場合

[System.Windows.Forms.Application]::EnableVisualStyles(); 
$form = new-object Windows.Forms.Form 
$form.Text = "Image Viewer" 
$form.Width = $img.Size.Width; 
$form.Height = $img.Size.Height; 
$pictureBox = new-object Windows.Forms.PictureBox 
$pictureBox.Width = $img.Size.Width; 
$pictureBox.Height = $img.Size.Height; 
$pictureBox.SizeMode = PictureBoxSizeMode.StretchImage 

これは素晴らしいことです。 これは数値です - 私はそれを知っています - PowerShellにそのことを知るにはどうすればよいですか?

ありがとうございます。

+1

'[System.Windows.Forms.PictureBoxSizeMode] :: StretchImage' – TessellatingHeckler

+0

あなただけの他のタイプのように、名前空間を必要としています。 – SLaks

+2

'$ pictureBox.SizeMode = 'StretchImage'' – PetSerAl

答えて

0

基本:

[System.Windows.Forms.PictureBoxSizeMode].GetEnumNames() 

アドバンスト(同等の数値と一緒に名):

[System.Enum]::GetNames([System.Windows.Forms.PictureBoxSizeMode])| 
    ForEach-Object {"{0} {1}" -f 
     [System.Windows.Forms.PictureBoxSizeMode]::$_.value__, $_ } 

はまたコードレビューGetting enum values of Pseudo-Enum classesに私の答えにGet-EnumValue機能(カスタムコマンドレット)を比較。

編集

powershell_ise[System.Windows.Forms.PictureBoxSizeMode]実行を含むすべての上記のコード。私たちは、System.Windows.Forms名前空間への参照を更新する必要が

 
PS D:\PShell> [System.Windows.Forms.PictureBoxSizeMode]::StretchImage 
Unable to find type [System.Windows.Forms.PictureBoxSizeMode]. Make sure that the assembly that contains this type is loaded. At line:1 char:1 
+ [System.Windows.Forms.PictureBoxSizeMode]::StretchImage 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (System.Windows.Forms.PictureBoxSizeMode:TypeName) [], RuntimeException 
    + FullyQualifiedErrorId : TypeNotFound 

;:残念ながら、powershellTypeNotFoundエラーが発生しますいくつかのソースは、同様にSystem.Drawingへの参照を更新助言:

 
PS D:\PShell> [void] (Add-Type -AssemblyName System.Windows.Forms -PassThru) 
PS D:\PShell> [void] (Add-Type -AssemblyName System.Drawing -PassThru) 
PS D:\PShell> [System.Windows.Forms.PictureBoxSizeMode]::StretchImage 
StretchImage 
PS D:\PShell> [System.Windows.Forms.PictureBoxSizeMode]::StretchImage.value__ 
1 
PS D:\PShell> 
+0

ええ、私は許されている値*が何であるかを知っています*私は、PowerShellに "StretchImage"をどんな数値にしてもらうことはできません。 –

+0

@EdChandlerの回答が更新されました。 – JosefZ

関連する問題