Casting a generic collection to base type

前端 未结 6 911
情歌与酒
情歌与酒 2020-12-03 22:46

I\'ve got an IList that I want to cast to ICollection but when I attempt an explicit cast, I get null

6条回答
  •  醉梦人生
    2020-12-03 23:46

    If .NET 3.5 is an option, you can use the Cast extension method in System.Core.dll to get a read-only IEnumerable for the collection:

    using System.Linq;
    ...
    private void Refresh() {
      PopulateList( cachedCollection.Cast( ) );
    }
    

提交回复
热议问题