简体   繁体   English

在 Out-GridView 中显示 WinForms object 属性

[英]Display WinForms object properties in Out-GridView

While testing I frequently want to check the properties of a WinForms object by piping the variable to Out-GridView, but it's not displaying properly.在测试时,我经常想通过将变量传递到 Out-GridView 来检查 WinForms object 的属性,但它没有正确显示。 Each property is showing up as a separate column.每个属性都显示为单独的列。 I was able to sort of get what I'm looking for by using a calculated property, but I have to reference the object by name within the calculation.通过使用计算属性,我能够得到我正在寻找的东西,但我必须在计算中按名称引用 object。 It also feels like maybe I'm making this harder than it needs to be.我也觉得也许我让这件事变得比需要的更难。 I'm pretty new with Powershell so please forgive my ineptitude.我对 Powershell 很陌生,所以请原谅我的无能。

Here's what I have at the moment:这是我目前所拥有的:

Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form | Get-Member -MemberType Property | Select-Object Name, @{Name='Value';Expression={$Form.($_.Name)}} | Out-GridView

It sounds like you want each property of the form to be shown on its own row in the grid view, instead of showing all properties in the columns of a single row, which happens by default.听起来您希望表单的每个属性都显示在网格视图中自己的行上,而不是在默认情况下在单行的列中显示所有属性。 To put it differently, you're looking for Format-List -like display instead of Format-Table -like display.换句话说,您正在寻找类似Format-List的显示而不是类似Format-Table的显示。

A simple solution is to use the intrinsic psobject property to reflect on an object's properties:一个简单的解决方案是使用内在的psobject属性来反映对象的属性:

Add-Type -AssemblyName System.Windows.Forms

$form = New-Object System.Windows.Forms.Form

$form.psobject.Properties | 
  Select-Object Name, Value |
  Out-GridView

Sample output - note how each property name-value pair is on its own row:示例 output - 请注意每个属性名称-值对如何位于其自己的行中:

out-gridview 屏幕截图

function Show-ObjectProperties([Parameter(ValueFromPipeline)]$F){
    ($F|Get-Member -M Property).Name|ForEach{@{$_=$F.$_}}|
    OGV -Title ($MyInvocation.Line -replace '^\$|\s*\|.+')
}


$MainForm = [Windows.Forms.Form]::new()
$SecondaryForm = [Windows.Forms.Form]::new()

$MainForm|Show-ObjectProperties
$SecondaryForm|Show-ObjectProperties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM