哈希表中的 WinForm
WinForm inside hashtable
我可以在哈希表中设置表单元素:
$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
Beginning in PowerShell 3.0, you can create an object from a hash
table of properties and property values.
The syntax is as follows:
[<class-name>]@{
<property-name>=<property-value>
<property-name>=<property-value>
…
}
This method works only for classes that have a null constructor,
that is, a constructor that has no parameters. The object properties
must be public and settable.
For more information, see about_Object_Creation.
检查第一个条件(没有参数的构造函数):
[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
System.Windows.Forms.Form
System.Windows.Forms.Label
System.Windows.Forms.Panel
检查后一个条件(对象属性必须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
)
}
}
Name Value
---- -----
System.Windows.Forms.Form {AcceptButton, AccessibleDefaultActionDescription, Acc...
System.Windows.Forms.Label {AccessibleDefaultActionDescription, AccessibleDescrip...
System.Windows.Forms.Panel {AccessibleDefaultActionDescription, AccessibleDescrip...
将以上两个代码片段放在一起是一项微不足道的任务……
我可以在哈希表中设置表单元素:
$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
Beginning in PowerShell 3.0, you can create an object from a hash table of properties and property values.
The syntax is as follows:
[<class-name>]@{ <property-name>=<property-value> <property-name>=<property-value> … }
This method works only for classes that have a null constructor, that is, a constructor that has no parameters. The object properties must be public and settable.
For more information, see about_Object_Creation.
检查第一个条件(没有参数的构造函数):
[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
System.Windows.Forms.Form System.Windows.Forms.Label System.Windows.Forms.Panel
检查后一个条件(对象属性必须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
)
}
}
Name Value ---- ----- System.Windows.Forms.Form {AcceptButton, AccessibleDefaultActionDescription, Acc... System.Windows.Forms.Label {AccessibleDefaultActionDescription, AccessibleDescrip... System.Windows.Forms.Panel {AccessibleDefaultActionDescription, AccessibleDescrip...
将以上两个代码片段放在一起是一项微不足道的任务……