public static bool TryCreate(object items, [NotNullWhen(true)] out CollectionTeaser?collectionTeaser) { collectionTeaser = null; if (items is IList list) { collectionTeaser = new CollectionTeaser(i => list.Add(i), list.Remove); } else if (items != null) { var itemsType = items.GetType(); var genericCollectionType = typeof(ICollection <>); //TODO, *IF* we really wanted to we could get the consumer to inform us of the correct type //if there are multiple impls. havent got time for this edge case right now though var collectionImplType = itemsType.GetInterfaces().SingleOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == genericCollectionType); if (collectionImplType != null) { var genericArgType = collectionImplType.GetGenericArguments().First(); var addMethodInfo = collectionImplType.GetMethod("Add", new[] { genericArgType }); var removeMethodInfo = collectionImplType.GetMethod("Remove", new[] { genericArgType }); collectionTeaser = new CollectionTeaser( i => addMethodInfo.Invoke(items, new[] { i }), i => removeMethodInfo.Invoke(items, new[] { i })); } } return(collectionTeaser != null); }
public static bool TryCreate(object items, out CollectionTeaser collectionTeaser) { collectionTeaser = null; var list = items as IList; if (list != null) { collectionTeaser = new CollectionTeaser(i => list.Add(i), list.Remove); } else if (items != null) { var itemsType = items.GetType(); var genericCollectionType = typeof (ICollection<>); //TODO, *IF* we really wanted to we could get the consumer to inform us of the correct type //if there are multiple impls. havent got time for this edge case right now though var collectionImplType = itemsType.GetInterfaces().SingleOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == genericCollectionType); if (collectionImplType != null) { var genericArgType = collectionImplType.GetGenericArguments().First(); var addMethodInfo = collectionImplType.GetMethod("Add", new[] {genericArgType}); var removeMethodInfo = collectionImplType.GetMethod("Remove", new[] { genericArgType }); collectionTeaser = new CollectionTeaser( i => addMethodInfo.Invoke(items, new[] {i}), i => removeMethodInfo.Invoke(items, new[] {i})); } } return collectionTeaser != null; }