powershell - 将字符串转换为数据类型以存储在哈希表中
问题描述
我的代码中有这样的东西:
[MVPSI.JAMS.CredentialRights]::Submit
我希望能够抽象它,这样我就可以有效地改变它的一部分,我希望它是一个字符串:
$typeName = "MVPSI.JAMS.CredentialRights"
$function = "Submit"
但是我不能这样做:
$typeName::$function
我该怎么做呢?公平地说,我什至不知道这些是什么特殊的[]
,并且::
在 .Net\PowerShell 中被调用。
解决方案
我什至不知道这些特殊的 [] 和 :: 在 .Net\PowerShell 中被称为什么
[...]
分隔类型文字;例如[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()
推荐阅读
- laravel - 我在 Laravel 和 vuejs 中的应用程序,当我部署到 VPS 时,在 vue 组件中 API 调用会转到 localhost 而不是我的服务器 IP 地址
- java - 启动应用程序后出错:org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration 上的错误处理条件
- django - 反向关系的Django verbose_name
- javascript - ScrollMagic 循环 - 场景持续时间与作为场景一部分的视频持续时间相同
- java - 如何在 java.util.ResourceBundle 中获取特定键并设置其值?
- machine-learning - 掩码和图像在 UNET 中如何相互配合?
- node.js - 将 pm2 与 npm run 一起使用
- c++ - 在 C++ 中使用字符串函数时,使用 substr() 函数终止
- javascript - Angular-6 显示和隐藏下拉选择选项未显示正确的数据
- android - Android 库矢量转换为默认混淆的 png 和 Kotlin 文件