示例#1
0
        /// <summary>
        /// Utility method that converts
        /// </summary>
        /// <param name="type"></param>
        /// <param name="listType"></param>
        /// <param name="instances"></param>
        /// <returns></returns>
        public static object ToList(this IEnumerable <ModelInstance> instances, ModelType type, Type listType)
        {
            ConstructorInfo ctor = listType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);

            if (ctor == null)
            {
                throw new Exception(string.Format("Could not find default constructor for list type \"{0}\".", listType.Name));
            }
            var result = ctor.Invoke(null);
            var list   = result as IList ?? type.ConvertToList(null, result);

            foreach (var instance in instances)
            {
                list.Add(instance.Instance);
            }
            return(result);
        }