AceDataObjectCollection

AceDataObjectCollection

我想为我拥有的自定义通用Colletion创建与.ToList ()类似的一组扩展名。这是我的物品AceDataObjectCollection<T> : ICollection<T> where T : IAceDataObjectIAceDataBlock : IAceDataObjectIAceMessage : IAceDataObject我想做这个。AceDataObjectCollection<IAceDataObject> objects = new AceDataObjectCollection<IAceDataObject>();objects.Add(...) // Add different data objects hereAceDataObjectCollection<IAceMessage> messages = objects.Where(o => o is IAceMessage).ToAceDataObjectCollection<IAceMessage>();我似乎无法弄清楚如何使用泛型正确地形成扩展。 最佳答案 我相信这就是您要寻找的。public static AceDataObjectCollection<T> ToAceDataObjectCollection<T>(this IEnumerable<T> col) where T : IAceDataObject{ AceDataObjectCollection<T> objects = new AceDataObjectCollection<T>(); foreach (T item in col) objects.Add(item); return objects;}关于c# - 通用扩展,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8053688/
10-11 09:05