我可以在哈希表中设置表单元素:
$Hash = @{}
$Hash.Main = New-Object System.Windows.Forms.Form
$Hash.Main.Left = 0
$Hash.Main.Top = 0
...
$Hash.Label = New-Object System.Windows.Forms.Label
$Hash.Label.Left = 0
$Hash.Label.Top = 0
...
$Hash.Panel = New-Object System.Windows.Forms.Panel
$Hash.Panel.Left = 0
$Hash.Panel.Top = 0
...
我该如何在哈希表中写入相同内容?我尽力使它成为可能。而且有效。但是这种语法正确吗?
$Hash = @{
Main = [System.Windows.Forms.Form] @{
Left = 0
Top = 0
...
}
Label = [System.Windows.Forms.Label] @{
Left = 0
Top = 0
...
}
Panel = [System.Windows.Forms.Panel] @{
Left = 0
Top = 0
...
}
}
谢谢
最佳答案
是的,此语法正确:
Creating Objects from Hash Tables
检查第一个条件(没有参数的构造函数):
[System.Drawing.Font], ### does not have empty constructor
[System.Windows.Forms.Form],
[System.Windows.Forms.Label],
[System.Windows.Forms.Panel] |
Where-Object {
'new' -in ( $_ |
Get-Member -MemberType Methods -Static |
Select-Object -ExpandProperty Name ) -and
$_::new.OverloadDefinitions -match ([regex]::Escape('new()'))
} | Select-Object -ExpandProperty FullName
检查后一种情况(对象属性必须是公共(public)的且可设置的):
[System.Windows.Forms.Form],
[System.Windows.Forms.Label],
[System.Windows.Forms.Panel] |
ForEach-Object {
@{ $_.FullName = (
$_.GetProperties('Instance,Public') | Where-Object CanWrite |
Select-Object -ExpandProperty Name | Sort-Object
)
}
}
将以上两个代码片段整合在一起是一项艰巨的任务……
关于winforms - 哈希表中的WinForm,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59973001/