将字符串转换为数据类型以存储在哈希表中



我在代码中有类似的东西:

 [MVPSI.JAMS.CredentialRights]::Submit

我希望能够抽象它,以便我可以有效地更改它的一部分,我希望它是一个字符串:

$typeName = "MVPSI.JAMS.CredentialRights"
$function = "Submit"

但是我不能这样做:

$typeName::$function

我该怎么做?公平地说,我什至不知道这些特殊的[]::在.net powershell中被称为

我什至不知道这些特别的[]和::在.net powershell

中被称为
  • [...]划分a type literal ;例如[MVPSI.JAMS.CredentialRights]

  • ::访问类型的静态成员

请注意,这两种语法表格都是特定于 powershell

使用类型文字的替代方法是将类型名称(字符串(铸成 [type]

# The type name as a string.
$typeName = 'MVPSI.JAMS.CredentialRights'
# Get a reference to the type by its name.
$type = [type] $typeName
# The name of the static method to call.
$function = 'Submit'
# Call the static method on the type by its name.
# Note: Omitting '()' will output the method *signature*, including
#       its overloads.
$type::$function()

不要在$ typename周围使用引号,因为您定义字符串而不是参考MVPSI.JAMS.CredentialRights类。改用括号。

$typeName = [MVPSI.JAMS.CredentialRights]
$function = "Submit"
$typeName::$function()

我相信::是给定类中该功能的静态调用。

最新更新