一些背景將是有益的,但它聽起來像是你有類似
class Foo<T> where T : Y {
object GetIndexer() { /* ... */ }
}
在這種情況下,爲什麼不只是
SomeCollection<Y> GetIndexer() { /* */ }
這樣,不需要強制轉換。
但是我對使用術語「索引器」有點困惑。對於C#索引器的重載[]運算符的類型的方式,這樣就可以做這樣的事情:
MyCollectionType c = new MyCollectionType()
c["someValue"]
他們像這樣定義:
class MyCollectionType {
public string this [string index] // This particular indexer maps strings to strings, but we could use other types if we wished.
get {} // Defines what to do when people do myCollection["foo"]
set {} // Defines what to do when people do myCollection["foo"] = "bar"
}
類型的對象SomeCollection<Y>
不是索引器,它是一個集合。
不幸的是索引器對象沒有實現IEnumerable接口,所以每個接口都沒有。 – 2009-10-21 20:37:41