I’m having trouble figuring out how to get Powershell to call an indexer on my class. The class looks something like this (vastly simplified):
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... } }
}
If I new up this class I can’t just use the bracket operator on it – I get an “unable to index” error. I also tried using get_item(), which is what Reflector shows me the indexer ultimately becomes in the assembly, but that gets me a “doesn’t contain a method named get_item” error.
UPDATE: this appears specific to my use of explicit interfaces. So my new question is: is there a way to get Powershell to see the indexer without switching away from explicit interfaces? (I really don’t want to modify this assembly if possible.)
This works for me (uses reflection):