如何消除 Powershell 中 Type.GetProperty 方法的歧义?

How to disambiguate the Type.GetProperty method in Powershell?

我正在尝试创建一个 Powershell 脚本来执行 C# 代码 in this answer 的操作。

当我排队时

$type = ($fieldlink).GetType()
$propInfo = $type.GetProperty("Default", [System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance)

我收到一个错误

Multiple ambiguous overloads found for "GetProperty" and the argument count: "2".

如何消除歧义并指定我想要采用字符串和 BindingFlags 的重载?

-bor 运算符 return 基础类型的结果(在本例中为 [int])而不是原始枚举类型。所以你必须将结果转换回 [System.Reflection.BindingFlags],尽管我更喜欢从字符串转换为枚举类型:

$propInfo = $type.GetProperty("Default", [System.Reflection.BindingFlags]'NonPublic, Instance')