我有一個通用函數,我想知道如何編寫。如何確定兩個對象的類型是否兼容?
List<Something> something;
public int countItems<T>(List<T> Items)
{
// Here I would like to compare the type of "Items" with the type of "something" to see if they are compatible. How do I do it?
return 0;
}
這真的取決於你的比較方法。什麼時候和Items一樣? – RoflcoptrException 2011-04-18 13:08:17
'var tAsSomething = Items [0] as Something;如果(tAsSomething!= null)...'是我最好的猜測,如果我理解正確。 – 2011-04-18 13:10:04
XY問題的陰影。第一條評論似乎是從字面上看原始問題標題的方式,所以我將標題改爲實際意義。 – 2017-10-22 00:08:12