2012-02-03 79 views
3

我已閱讀了許多關於此的帖子,但我仍然不確定是否完全理解了定義。打開,關閉,綁定和未綁定泛型類型

以下是我認爲是不同術語的例子。我在這裏的正確軌道,還是我還不理解的概念。謝謝

Array<T TArray> - unbound and open. 
Array<int> - bound and closed. 
Array<Array<T TArray> - bound and open. 
Array<Array<int>> - bound and closed. 

回答

3

沒有約束意味着像typeof(Dictionary<,>)。未綁定類型僅適用於Reflection,只能在typeof()中使用,而不能在其他任何上下文中使用。 所有無約束類型都是封閉類型,「無約束和開放」組合是不可能的。

假設T是當前類/方法的類型參數:

Dictionary<,> - unbound and closed 
Dictionary<string, int> - constructed and closed 
Dictionary<int, T> - constructed and open 
Dictionary<string, List<T>> - constructed and open 
NonGenericClass - bound and closed 

注意,有作爲List<Dictionary<,>>沒有這樣的事情 - 未結合的類型不能在typeof()被用作類型參數,僅直接。一種類型或者是未綁定的,或者是完全綁定的。 如果一個類型是未綁定的,那麼就沒有可以引用類型參數的地方,所以「unbound and open」組合是不可能的。

+0

謝謝丹尼爾。很有幫助。 – 2012-02-09 16:54:41