【问题标题】:Why no static indexers? [duplicate]为什么没有静态索引器? [复制]
【发布时间】:2009-06-01 18:28:58
【问题描述】:

可能的重复:
How to reference an indexer member of a class in C# comments
Static Indexers?


就目前而言,我必须像这样编写我的代码

internal class ReportMap {

    static internal ReportMap Instance {
        get { return reportMap; }
    }

    internal Type this[char c] {
        get { return data[c]; }
    }

    // private parts
    static Dictionary<char, Type> data =
        new Dictionary<char, Type>();

    ReportMap() {
        data.Add('S', typeof(StatusReport));
        data.Add('Q', typeof(QualityReport));
    }

    static ReportMap reportMap = new ReportMap();
}

并访问索引:

Type t = ReportMap.Instance[aChar]; // eww

这种能力被排除在语言之外是否有特定的原因?特别是,它会被认为是糟糕的设计吗?对此已经发布了另一个问题,但不幸的是,我没有足够的代表发表评论。

【问题讨论】:

标签: c# static-indexers


【解决方案1】:

这是一个很好的欺骗参考。具体来说,查看答案:-) Static Indexers?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 2016-10-21
    • 2012-06-10
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 2010-09-28
    相关资源
    最近更新 更多