示例#1
0
        /// <summary>
        /// Gets the <see cref="ColumnInfo"/> for the specified column name.
        /// </summary>
        /// <param name="name">The column name.</param>
        /// <returns>A <see cref="ColumnInfo"/> object or null if no <see cref="ColumnInfo"/> exists for the specified column name.</returns>
        public ColumnInfo GetColumnByName(string name)
        {
            ColumnInfo col;

            ColumnsByName.TryGetValue(name, out col);
            return(col);
        }
示例#2
0
        /// <summary>
        /// Creates an <see cref="ExpandoObject"/> object that includes type mapping information.
        /// </summary>
        /// <returns>An <see cref="ExpandoObject"/> object.</returns>
        public ExpandoObject CreateExpando()
        {
            var eo      = new ExpandoObject();
            var expando = (IDictionary <string, object>)eo;
            var map     = ColumnsByName.ToDictionary(c => c.Key, c => ColumnsByIndex.First(ci => ci.Value.First() == c.Value.First()).Key);

            expando[IndexMapPropertyName] = map;

            return(eo);
        }
示例#3
0
 /// <summary>
 /// Gets the <see cref="ColumnInfo"/> for the specified column name.
 /// </summary>
 /// <param name="name">The column name.</param>
 /// <returns>A <see cref="ColumnInfo"/> object or null if no <see cref="ColumnInfo"/> exists for the specified column name.</returns>
 public List <ColumnInfo> GetColumnByName(string name)
 {
     ColumnsByName.TryGetValue(name, out List <ColumnInfo> col);
     return(col);
 }
示例#4
0
        void Analyze()
        {
            foreach (var prop in Type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (!(Attribute.GetCustomAttribute(prop, typeof(IgnoreAttribute)) is IgnoreAttribute))
                {
                    var ci = new ColumnInfo(prop); // Both direction

                    var attribs = Attribute.GetCustomAttributes(prop, typeof(ColumnAttribute)).Cast <ColumnAttribute>();
                    if (attribs.Any())
                    {
                        foreach (var columnAttribute in attribs)
                        {
                            ci = new ColumnInfo(prop); // Both direction
                            if (!string.IsNullOrEmpty(columnAttribute.Name))
                            {
                                if (!ColumnsByName.ContainsKey(columnAttribute.Name))
                                {
                                    ColumnsByName.Add(columnAttribute.Name, new List <ColumnInfo>());
                                }

                                ColumnsByName[columnAttribute.Name].Add(ci);
                            }
                            else if (!ColumnsByName.ContainsKey(prop.Name))
                            {
                                ColumnsByName.Add(prop.Name, new List <ColumnInfo>()
                                {
                                    ci
                                });
                            }

                            if (columnAttribute.Index > 0)
                            {
                                var idx = columnAttribute.Index - 1;
                                if (!ColumnsByIndex.ContainsKey(idx))
                                {
                                    ColumnsByIndex.Add(idx, new List <ColumnInfo>());
                                }

                                ColumnsByIndex[idx].Add(ci);
                            }

                            ci.Directions = columnAttribute.Directions;
                        }
                    }
                    else if (!ColumnsByName.ContainsKey(prop.Name))
                    {
                        ColumnsByName.Add(prop.Name, new List <ColumnInfo>()
                        {
                            ci
                        });
                    }

                    if (Attribute.GetCustomAttribute(prop, typeof(DataFormatAttribute)) is DataFormatAttribute dataFormatAttribute)
                    {
                        ci.BuiltinFormat = dataFormatAttribute.BuiltinFormat;
                        ci.CustomFormat  = dataFormatAttribute.CustomFormat;
                    }

                    if (Attribute.GetCustomAttribute(prop, typeof(FormulaResultAttribute)) is FormulaResultAttribute)
                    {
                        ci.FormulaResult = true;
                    }

                    if (Attribute.GetCustomAttribute(prop, typeof(JsonAttribute)) is JsonAttribute)
                    {
                        ci.Json = true;
                    }
                }
            }
        }
示例#5
0
        void Analyze()
        {
            var props = Type.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            foreach (var prop in props)
            {
                if (!(Attribute.GetCustomAttribute(prop, typeof(IgnoreAttribute)) is IgnoreAttribute))
                {
                    var ci = new ColumnInfo(prop);

                    var attribs = Attribute.GetCustomAttributes(prop, typeof(ColumnAttribute)).Cast <ColumnAttribute>();
                    if (attribs.Any())
                    {
                        foreach (var columnAttribute in attribs)
                        {
                            ci = new ColumnInfo(prop);
                            if (!string.IsNullOrEmpty(columnAttribute.Name))
                            {
                                if (!ColumnsByName.ContainsKey(columnAttribute.Name))
                                {
                                    ColumnsByName.Add(columnAttribute.Name, new List <ColumnInfo>());
                                }

                                ColumnsByName[columnAttribute.Name].Add(ci);
                            }
                            else if (!ColumnsByName.ContainsKey(prop.Name))
                            {
                                ColumnsByName.Add(prop.Name, new List <ColumnInfo>()
                                {
                                    ci
                                });
                            }

                            if (columnAttribute.Index > 0)
                            {
                                var idx = columnAttribute.Index - 1;
                                if (!ColumnsByIndex.ContainsKey(idx))
                                {
                                    ColumnsByIndex.Add(idx, new List <ColumnInfo>());
                                }

                                ColumnsByIndex[idx].Add(ci);
                            }

                            ci.Directions = columnAttribute.Directions;
                        }
                    }
                    else if (!ColumnsByName.ContainsKey(prop.Name))
                    {
                        ColumnsByName.Add(prop.Name, new List <ColumnInfo>()
                        {
                            ci
                        });
                    }

                    if (Attribute.GetCustomAttribute(prop, typeof(DataFormatAttribute)) is DataFormatAttribute dataFormatAttribute)
                    {
                        ci.BuiltinFormat = dataFormatAttribute.BuiltinFormat;
                        ci.CustomFormat  = dataFormatAttribute.CustomFormat;
                    }

                    if (Attribute.GetCustomAttribute(prop, typeof(FormulaResultAttribute)) is FormulaResultAttribute)
                    {
                        ci.FormulaResult = true;
                    }

                    if (Attribute.GetCustomAttribute(prop, typeof(JsonAttribute)) is JsonAttribute)
                    {
                        ci.Json = true;
                    }
                }
            }

            var hasDefaultConstructor = Type.IsValueType || Type.GetConstructor(Type.EmptyTypes) != null;

            if (!hasDefaultConstructor)
            {
                Constructor = Type.GetConstructors(BindingFlags.Public | BindingFlags.Instance)
                              .OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();

                if (Constructor == null)
                {
                    throw new ExcelMapperConvertException($"Type {Type.FullName} has no suitable constructor.");
                }

                ConstructorParams = Constructor.GetParameters()
                                    .Select((p, i) => (Param: p, Index: i, HasProp: props.Any(r => string.Equals(r.Name, p.Name, StringComparison.OrdinalIgnoreCase))))
                                    .Where(p => p.HasProp)
                                    .ToDictionary(p => p.Param.Name, p => p.Param, StringComparer.OrdinalIgnoreCase);
            }
        }