我有一個方法,需要輸入參數IEnumerable
類型的對象。我想枚舉枚舉併爲每個項目使用反射來獲取每個屬性的值。爲什麼Type.GetElementType返回null?
我有以下代碼:
protected void WriteData(IEnumerable data)
{
var enumerationTypeInfo = data.GetType();
var itemTypeInfo = enumerationTypeInfo.GetElementType();
...
}
問題是enumerationTypeInfo.GetElementType()
總是返回null
。特別是,我將List<Entry>
傳入WriteData
,其中Entry
是我創建的類。當我使用調試器並設置一個斷點時,我可以看到enumerationTypeInfo
正確顯示它是一個類型爲Entry
的列表,但爲什麼GetElementType
返回null
?
感謝
此外 - 如果用於泛型類型,GetElementType將始終返回null。 請參閱MSDN文章http://msdn.microsoft.com/en-us/library/system.type.getelementtype.aspx – EtherDragon