带有外部列表的 Add-Type 枚举
Add-Type enum with an external list
我使用 PS 4.0,我有一个带有函数参数枚举的脚本。
Add-Type -TypeDefinition @"
public enum AppEnum
{
MyFirstItem,
MySecondItem
}
"@
我是这样用的:
Function MyFunction
{
Param
(
[AppEnum]$App
)
Return $App
}
但是我的枚举列表与我的 XML 文件中的相同,可以修改。
我这样检索我的列表:
($xcf.Apps.W2008.LAN | Get-Member -MemberType Properties).Name | ?{$_ -ne "Repository"}
我想使用从我的 XML 文件中提取的这个动态列表作为我在 "Add-Type" 中的枚举的列表,但我不知道如何做,也不知道是否可行.你知道怎么做吗?
# Get list from your XML file here
$myEnumList = @("MyFirstItem", "MySecondItem")
$source = "public enum AppEnum { " + ($myEnumList | %{ $_ + "," }) + " }"
Add-Type -TypeDefinition $source
# Let's check value is known
[AppEnum]::MyFirstItem
我使用 PS 4.0,我有一个带有函数参数枚举的脚本。
Add-Type -TypeDefinition @"
public enum AppEnum
{
MyFirstItem,
MySecondItem
}
"@
我是这样用的:
Function MyFunction
{
Param
(
[AppEnum]$App
)
Return $App
}
但是我的枚举列表与我的 XML 文件中的相同,可以修改。
我这样检索我的列表:
($xcf.Apps.W2008.LAN | Get-Member -MemberType Properties).Name | ?{$_ -ne "Repository"}
我想使用从我的 XML 文件中提取的这个动态列表作为我在 "Add-Type" 中的枚举的列表,但我不知道如何做,也不知道是否可行.你知道怎么做吗?
# Get list from your XML file here
$myEnumList = @("MyFirstItem", "MySecondItem")
$source = "public enum AppEnum { " + ($myEnumList | %{ $_ + "," }) + " }"
Add-Type -TypeDefinition $source
# Let's check value is known
[AppEnum]::MyFirstItem