Exemplo n.º 1
0
        public void MapRow(IDataWrapper fromRow, int rowNumber, T toObject)
        {
            var columnMap = fromRow.GetColumnMap();

            foreach (string databaseFieldName in columnMap.Keys)
            {
                if (_NamePropMap.ContainsKey(databaseFieldName))
                {
                    int          indexOfField = columnMap[databaseFieldName];
                    PropertyInfo pi           = _NamePropMap[databaseFieldName].PropertyInfo;
                    object       newObject    = null;
                    if (!fromRow.IsFieldNullByIndex(rowNumber, indexOfField))
                    {
                        object objToConvert = fromRow.GetValueByIndex(rowNumber, indexOfField);
                        if (objToConvert is DateTime)
                        {
                            newObject = fromRow.GetDateByFieldName(rowNumber, indexOfField);
                        }
                        else if (objToConvert.GetType() == typeof(byte[]) && pi.PropertyType == typeof(string))
                        {
                            newObject = HexStr(objToConvert as byte[]);
                        }
                        else if (ValidateJson(objToConvert.ToString()))
                        {
                            try
                            {
                                newObject = JsonConvert.DeserializeObject(objToConvert.ToString(),
                                                                          _NamePropMap[databaseFieldName].PropertyInfo.PropertyType);
                            }
                            catch
                            {
                                newObject = objToConvert.ToString();
                            }
                        }
                        else if (pi.PropertyType.IsEnum)
                        {
                            if (objToConvert.GetType() == typeof(string))
                            {
                                newObject = Enum.Parse(pi.PropertyType, objToConvert.ToString());
                            }
                            else
                            {
                                newObject = Enum.ToObject(pi.PropertyType, Convert.ToInt32(objToConvert));
                            }
                        }
                        else
                        {
                            if (pi.PropertyType.IsGenericType &&
                                pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                            {
                                if (objToConvert == null)
                                {
                                    newObject = null;
                                }
                                else
                                {
                                    Type underType = Nullable.GetUnderlyingType(pi.PropertyType);
                                    newObject = Convert.ChangeType(
                                        objToConvert,
                                        underType);
                                }
                            }
                            else
                            {
                                newObject = Convert.ChangeType(
                                    objToConvert,
                                    pi.PropertyType);
                            }
                        }
                    }

                    toObject.GetType().InvokeMember(
                        pi.Name,
                        BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
                        Type.DefaultBinder,
                        toObject,
                        new[] { newObject });
                }
            }
        }