コード例 #1
0
        public static T CreateInstance <T>(this IChoCSVLiteReader parser)
        {
            var rec = ChoActivator.CreateInstance <T>();

            return(rec);
        }
コード例 #2
0
        public static T Map <T>(this IChoCSVLiteReader parser, int lineNo, string[] cols, Action <int, string[], T> mapper,
                                bool hasHeader, ref bool skip, ref string[] headers, bool positionalMapping) where T : new()
        {
            skip = false;
            if (mapper != null)
            {
                if (hasHeader)
                {
                    if (lineNo == 0)
                    {
                        skip = true;
                        return(default(T));
                    }
                }

                var rec = parser.CreateInstance <T>();
                mapper(lineNo, cols, rec);
                return(rec);
            }
            else
            {
                if (hasHeader)
                {
                    if (lineNo == 0)
                    {
                        headers = cols.Select((c, i) => c).ToArray();
                        skip    = true;
                        return(default(T));
                    }
                }
                else
                {
                    if (lineNo == 0)
                    {
                        headers = cols.Select((c, i) => $"Column{i}").ToArray();
                    }
                }

                var recType = typeof(T);
                if (recType == typeof(ChoDynamicObject) || recType == typeof(ExpandoObject))
                {
                    if (recType == typeof(ChoDynamicObject))
                    {
                        var     rec  = parser.CreateInstance <T>();
                        dynamic dObj = rec;

                        var dict = headers.Select((h, i) => new { Key = h, Value = cols[i] }).ToDictionary(kvp => kvp.Key, kvp => (object)kvp.Value);
                        dObj.SetDictionary(dict);
                        return(rec);
                    }
                    else
                    {
                        var expando    = parser.CreateInstance <T>();
                        var expandoDic = (IDictionary <string, object>)expando;
                        int index      = 0;
                        foreach (var header in headers)
                        {
                            expandoDic.Add(header, cols[index++]);
                        }

                        return(expando);
                    }
                }
                else
                {
                    var rec = parser.CreateInstance <T>();

                    if (positionalMapping)
                    {
                        PropertyInfo[] props    = GetPropertyInfos <T>();
                        int            index    = 0;
                        string         col      = null;
                        Type           propType = null;
                        foreach (var prop in props)
                        {
                            if (index < cols.Length)
                            {
                                propType = props[index].PropertyType;
                                col      = cols[index++];
                                ChoType.SetPropertyValue(rec, prop, ChoConvert.ConvertTo(col, propType));
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        Dictionary <string, PropertyInfo> propDict = GetPropertyInfoDictionary <T>();

                        int          index    = 0;
                        string       col      = null;
                        Type         propType = null;
                        PropertyInfo prop     = null;
                        foreach (var header in headers)
                        {
                            if (propDict.ContainsKey(header))
                            {
                                prop = propDict[header];
                                if (prop != null)
                                {
                                    propType = prop.PropertyType;
                                    col      = cols[index++];
                                    ChoType.SetPropertyValue(rec, prop, ChoConvert.ConvertTo(col, propType));
                                }
                            }
                        }
                    }
                    return(rec);
                }
            }
        }