Esempio n. 1
0
        object ReadField(FieldInfo fi, ExcelToObjectAttribute attr)
        {
            string tablePath = fi.Name;

            if (attr != null && attr.TablePath != null)
            {
                tablePath = attr.TablePath;
            }

            Type fieldType = fi.FieldType;

            if (fieldType.Name == "List`1")
            {
                Type itemType = fieldType.GetGenericArguments()[0];

                return(ReadList(fieldType, tablePath, itemType, attr));
            }
            else if (fieldType.Name == "Dictionary`2")
            {
                var subTypes = fieldType.GetGenericArguments();

                Type keyType   = subTypes[0];
                Type valueType = subTypes[1];

                return(ReadDictionary(fieldType, tablePath, valueType, attr));
            }
            else
            {
                return(ReadSingle(fieldType, tablePath, attr));
            }
        }
Esempio n. 2
0
        public bool MapInto(object destObj)
        {
            bool hasError = false;

            foreach (var fi in destObj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                var attrs = fi.GetCustomAttributes(typeof(ExcelToObjectAttribute), false);
                ExcelToObjectAttribute attr = attrs.Length > 0 ? (ExcelToObjectAttribute)attrs[0] : null;

                if (attr != null && attr.Ignore == true)
                {
                    continue;
                }

                try
                {
                    object fieldValue = ReadField(fi, attr);
                    fi.SetValue(destObj, fieldValue);
                }
                catch (Exception)
                {
                    hasError = true;
                }
            }

            return(!hasError);
        }
Esempio n. 3
0
        public void MapInto(object destObj)
        {
            StringBuilder error = null;

            foreach (var fi in destObj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                var attrs = fi.GetCustomAttributes(typeof(ExcelToObjectAttribute), false);
                ExcelToObjectAttribute attr = attrs.Length > 0 ? (ExcelToObjectAttribute)attrs[0] : ExcelToObjectAttribute.Default;

                if (attr.Ignore == true)
                {
                    continue;
                }

                try
                {
                    string tableName = fi.Name;
                    if (attr.TableName != null)
                    {
                        tableName = attr.TableName;
                    }

                    if (attr.MapInto)
                    {
                        // recursively map into sub-object
                        object subObj = Util.New(fi.FieldType);

                        MapInto(subObj);

                        fi.SetValue(destObj, subObj);
                    }
                    else
                    {
                        var table = this[tableName];
                        if (table != null)
                        {
                            object fieldValue = table.ReadField(fi, attr.DictionaryKeyName);
                            fi.SetValue(destObj, fieldValue);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (error == null)
                    {
                        error = new StringBuilder();
                    }

                    error.AppendFormat("[{0}:{1}] ", fi.Name, ex.Message);
                }
            }

            if (error != null)
            {
                throw new Exception(error.ToString());
            }
        }
Esempio n. 4
0
        object ReadList(Type listType, string tablePath, Type itemType, ExcelToObjectAttribute attr)
        {
            object listObj = Util.New(listType);

            var context = new ExcelReader.ReadContext();

            bool retn = mReader.ReadListInternal(tablePath, itemType, context, listObj as IList);

            return(retn ? listObj : null);
        }
Esempio n. 5
0
        object ReadSingle(Type itemType, string tablePath, ExcelToObjectAttribute attr)
        {
            List <object> listObj = new List <object>();

            var context = new ExcelReader.ReadContext()
            {
                readMaxRows = 1
            };

            bool retn = mReader.ReadListInternal(tablePath, itemType, context, listObj);

            return(retn ? listObj[0] : null);
        }
Esempio n. 6
0
        object ReadDictionary(Type dicType, string tablePath, Type valueType, ExcelToObjectAttribute attr)
        {
            object dicObj = Util.New(dicType);

            var context = new ExcelReader.ReadContext();

            ExcelReader.KeySelector keySelector = null;
            if (attr != null && attr.DictionaryKeyName.IsValid())
            {
                keySelector = ExcelReader.KeySelector.From(valueType, attr.DictionaryKeyName);
            }

            bool retn = mReader.ReadDictionaryInternal(tablePath, valueType, context, dicObj as IDictionary, keySelector);

            return(retn ? dicObj : null);
        }