Example #1
0
        public static T ToInstance <T>(this Table table, CultureInfo cultureInfo = null)
        {
            table = AdjustTableToHorizontal(table);
            var headerArray          = (string[])table.Header;
            var typeCreationInfo     = TypeCreationInfoCache.Get(typeof(T));
            var headerForConstructor = GetHeadersForConstructorMatch(headerArray);
            var constructor          = GetBestConstructorFor(typeCreationInfo.ConstructorOptions, headerForConstructor);

            var(values, tables) = constructor.IsDefaultConstructor ? GetParameterArraysFor(typeCreationInfo.PropertyOptions, headerForConstructor, headerArray, table.Rows[0]) : GetParameterArraysFor(constructor.Parameters, headerForConstructor, headerArray, table.Rows[0]);
            return(constructor.GetFunc <T>().Invoke(values, tables, cultureInfo ?? CultureInfo.InvariantCulture));
        }
Example #2
0
        public static List <T> ToSet <T>(this Table table, CultureInfo cultureInfo = null)
        {
            var headerArray = (string[])table.Header;

            EnsureHorizontalTable(table, headerArray);
            var typeCreationInfo     = TypeCreationInfoCache.Get(typeof(T));
            var headerForConstructor = GetHeadersForConstructorMatch(headerArray);
            var constructor          = GetBestConstructorFor(typeCreationInfo.ConstructorOptions, headerForConstructor);

            cultureInfo ??= CultureInfo.InvariantCulture;
            var ctorFunc = constructor.GetFunc <T>();
            var result   = new List <T>(table.Rows.Count);

            for (var index = 0; index < table.Rows.Count; index++)
            {
                var(values, tables) = constructor.IsDefaultConstructor ? GetParameterArraysFor(typeCreationInfo.PropertyOptions, headerForConstructor, headerArray, table.Rows[index]) : GetParameterArraysFor(constructor.Parameters, headerForConstructor, headerArray, table.Rows[index]);
                result.Add(ctorFunc.Invoke(values, tables, cultureInfo));
            }

            return(result);
        }