如何从PowerShell调用C#类的索引器?
发布时间:2020-12-15 07:54:15 所属栏目:百科 来源:网络整理
导读:我无法弄清楚如何让Power shell在我的课堂上调用索引器.该类看起来像这样(大大简化): public interface IIntCounters{ int this[string counterName] { get; set; }}public class MyClass : IIntCounters{ public IIntCounters Counters { get { return thi
我无法弄清楚如何让Power
shell在我的课堂上调用索引器.该类看起来像这样(大大简化):
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... } } } 如果我新上了这个类,我不能只使用括号运算符 – 我得到一个“无法索引”错误.我也尝试使用get_item(),这是Reflector向我展示的索引器最终变成了程序集,但这让我得到了“不包含名为get_item的方法”错误. 更新:这似乎特定于我使用显式接口.所以我的新问题是:有没有办法让Powershell在不切换显式接口的情况下看到索引器? (如果可能,我真的不想修改这个程序集.) 解决方法
这对我有用(使用反射):
$obj = new-object MyClass $p = [IIntCounters].GetProperty("Item") $p.GetValue($obj,@("counterName")) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |