예제 #1
0
        /// WARNING: Use only if T is a value type.
        ///
        /// Returns the List's underlying array. Modifications made to it will
        /// be reflected in the list.
        ///
        /// The reflection version of this function is not especially fast; with
        /// the added overhead, the break-even point for iterating over a []
        /// instead of a List<> while doing a tiny amount of work (eg a vector
        /// dot + multiply) is about about 40 elements.
        /// TODO: investigate https://mattwarren.org/2016/12/14/Why-is-Reflection-slow/
        ///
        /// The real win is getting to use Array.Copy, pointers, etc.
        public static T[] GetBackingArray <T>(this List <T> list)
        {
            var pub = ConvertHelper <List <T>, PublicList <T> > .Convert(list);

            if (pub != null)
            {
                return(pub.BackingArray);
            }
            else
            {
                return((T[])sm_items.Get(typeof(List <T>)).GetValue(list));
            }
        }
예제 #2
0
        public static void SetBackingArray <T>(this List <T> list, T[] value)
        {
            var pub = ConvertHelper <List <T>, PublicList <T> > .Convert(list);

            if (pub != null)
            {
                pub.BackingArray = value;
            }
            else
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                sm_items.Get(typeof(List <T>)).SetValue(list, value);
                sm_size.Get(typeof(List <T>)).SetValue(list, value.Length);
            }
        }