예제 #1
0
        public static List <T> GetUnderlyingItemsAsList <T>(System.Collections.ObjectModel.Collection <T> coll, bool throwOnError = true)
        {
            if (coll == null)
            {
                return(null);
            }

            var collType = coll.GetType();
            Func <object, object> getFunc;

            lock (gCollectionGetUnderlyingListFuncs)
            {
                if (!gCollectionGetUnderlyingListFuncs.TryGetValue(collType, out getFunc))
                {
                    getFunc = Reflection.Util.GenerateMemberGetter <object>(collType, "items");
                    gCollectionGetUnderlyingListFuncs.Add(collType, getFunc);
                }
            }

            var items = getFunc(coll);
            var list  = items as List <T>;

            if (list == null && throwOnError)
            {
                throw new InvalidOperationException(string.Format(KSoft.Util.InvariantCultureInfo,
                                                                  "Tried to get Collection's underling Items as a List<{0}> but it is a {1}",
                                                                  typeof(T), items?.GetType()));
            }

            return(list);
        }