powershellcastingexplicit-interface

How can I call explicitly implemented interface method from PowerShell?


Code:

add-type @"
    public interface IFoo
    {
        void Foo();
    }

    public class Bar : IFoo
    {
        void IFoo.Foo()
        {
        }
    }
"@ -Language Csharp

$bar = New-Object Bar
($bar -as [IFoo]).Foo() # ERROR.

Error:

Method invocation failed because [Bar] doesn't contain a method named 'Foo'.


Solution

  • Bad news: It's a bug.

    https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=249840&SiteID=99