如何从PowerShell调用C#类的索引器?

sco*_*obi 7 c# powershell

我无法弄清楚如何让Powershell在我的课堂上调用索引器.该类看起来像这样(大大简化):

public interface IIntCounters
{
    int this[string counterName] { get; set; }
}

public class MyClass : IIntCounters
{
    public IIntCounters Counters { get { return this; } }

    int IIntCounters.this[string counterName]
        { get { ...return something... } }
}
Run Code Online (Sandbox Code Playgroud)

如果我新上了这个类,我不能只使用它上面的括号运算符 - 我得到一个"无法索引"的错误.我也尝试使用get_item(),这是Reflector向我展示的索引器最终变成了程序集,但这让我得到了"不包含名为get_item的方法"错误.

更新:这似乎特定于我使用显式接口.所以我的新问题是:有没有办法让Powershell在不切换显式接口的情况下看到索引器?(如果可能,我真的不想修改这个程序集.)

dvd*_*rle 5

这对我有用(使用反射):

$obj = new-object MyClass
$p = [IIntCounters].GetProperty("Item")
$p.GetValue($obj, @("counterName"))
Run Code Online (Sandbox Code Playgroud)