How to disambiguate the Type.GetProperty method in Powershell?

333 Views Asked by At

I am trying to create a Powershell script that does what the C# code in this answer does.

When I get to the line

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

I'm getting an error

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

How do I disambiguate and specify that I want the overload that takes a string and BindingFlags?

1

There are 1 best solutions below

1
On BEST ANSWER

-bor operator return result of underlying type ([int] in this case) rather then original enum type. So you have to cast result back to [System.Reflection.BindingFlags], although I prefer to cast to enum type from string:

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