有人可以解釋爲什麼下面的代碼會引發以下異常:將一個集合<T>投射到一個界面 - 爲什麼它不工作?
InvalidCastException: Unable to cast object of type 'System.Collections.ObjectModel.Collection'1[UserQuery+Test]' to type 'System.Collections.ObjectModel.Collection'1[UserQuery+ITest]'.
public interface ITest
{
}
public class Test : ITest
{
}
void Main()
{
Collection<Test> t = new Collection<Test>();
t.Add(new UserQuery.Test());
var casted = (Collection<ITest>)t.Cast<ITest>();
}
的簽名Cast<T>
意味着T
輸出,但事實並非如此。到底是怎麼回事? covariance
與它有什麼關係?
嗯,這可能會或可能不會創建一個新的'的IEnumerable'。如果不需要,它會進行優化。見http://msmvps.com/blogs/jon_skeet/archive/2011/01/13/reimplementing-linq-to-objects-part-33-cast-and-oftype.aspx –
@JonSkeet非常真實 - 但它更安全假設它不會與你傳遞的同一個集合類型... –
假設可能發生 - 對結果的操作可能會影響源或不是,所以無論哪種方式對壞的假設都有危險是比較安全的。 –