예제 #1
0
        private static T CreateItemFromRow <T>(ExcelRange row, IList <PropertyInfo> properties) where T : new()
        {
            T   item  = new T();
            int index = 0;

            //ignore not mapped attribute
            properties = properties.Where(x => x.GetCustomAttribute <NotMappedAttribute>() == null).ToList();

            foreach (var property in properties)
            {
                string value;
                try
                {
                    value = row.ElementAt(index).Text;
                    if (property.PropertyType == typeof(System.DayOfWeek))
                    {
                        DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), value);
                        property.SetValue(item, day, null);
                    }
                    else if (property.PropertyType == typeof(System.DateTime) || property.PropertyType == typeof(Nullable <System.DateTime>))
                    {
                        if (!String.IsNullOrWhiteSpace(value))
                        {
                            DateTime date = DateTime.Parse(value);
                            property.SetValue(item, date, null);
                        }
                        else
                        {
                            property.SetValue(item, null, null);
                        }
                    }
                    else if (property.PropertyType == typeof(int) || property.PropertyType == typeof(Nullable <int>))
                    {
                        if (value != null)
                        {
                            property.SetValue(item, int.Parse(value), null);
                        }
                        else
                        {
                            property.SetValue(item, null, null);
                        }
                    }
                    else if (property.PropertyType == typeof(bool) || property.PropertyType == typeof(Nullable <int>))
                    {
                        if (value != null)
                        {
                            bool _newValue = false;
                            int  dump;
                            if (int.TryParse(value, out dump))
                            {
                                _newValue = dump == 0 ? false : true;
                            }
                            else
                            {
                                _newValue = value.ToString().ToLower().Equals("true") ? true : false;
                            }
                            property.SetValue(item, _newValue, null);
                        }
                        else
                        {
                            property.SetValue(item, null, null);
                        }
                    }
                    else
                    {
                        property.SetValue(item, value, null);
                    }
                }
                catch
                {
                    property.SetValue(item, null, null);
                }


                index++;
            }

            return(item);
        }