示例#1
0
        private Type GetDynamicType(ISheet sheet)
        {
            var firstRowIndex = sheet.FirstRowNum;
            var firstRow      = sheet.GetRow(firstRowIndex);

            var names = new Dictionary <string, Type>();

            foreach (var header in firstRow)
            {
                var column = GetColumnInfoByDynamicAttribute(header);
                var type   = Helper.InferColumnDataType(sheet, HasHeader ? sheet.FirstRowNum : -1, header.ColumnIndex);

                if (column != null)
                {
                    names[column.Attribute.PropertyName] = type ?? typeof(string);
                }
                else
                {
                    var headerValue = GetHeaderValue(header);
                    var tempColumn  = new ColumnInfo(headerValue, header.ColumnIndex, null);
                    if (_columnFilter != null && !_columnFilter(tempColumn))
                    {
                        continue;
                    }

                    string propertyName;
                    if (HasHeader && MapHelper.GetCellType(header) == CellType.String)
                    {
                        propertyName = MapHelper.GetVariableName(header.StringCellValue, IgnoredNameChars,
                                                                 TruncateNameFrom, header.ColumnIndex);
                    }
                    else
                    {
                        propertyName = MapHelper.GetVariableName(null, null, null, header.ColumnIndex);
                    }

                    names[propertyName]             = type ?? typeof(string);
                    DynamicAttributes[propertyName] = new ColumnAttribute((ushort)header.ColumnIndex)
                    {
                        PropertyName = propertyName
                    };
                }
            }

            return(AnonymousTypeFactory.CreateType(names, true));
        }
        private Expression Build(IDictionary <string, Expression> bindings)
        {
            if (NewType == null)
            {
                NewType = AnonymousTypeFactory.CreateAnonymousType
                          (
                    bindings.ToDictionary(k => k.Key, v => v.Value.Type)
                          );
            }

            return(Expression.MemberInit
                   (
                       Expression.New(NewType),
                       bindings.Select
                       (
                           binding => Expression.Bind(NewType.GetMemberInfo(binding.Key), binding.Value)
                       )
                   ));
        }