Example #1
0
        /// <summary>
        /// Create and object and fill it's mapped properties through a DataRow
        /// </summary>
        /// <param name="dr">DataRow with columns mapped to the object type</param>
        /// <returns>A new instance of the object type with the filled properties</returns>
        public T CreateObject <T>(DataRow dr, TarkTypeMapping <T> typeMapping)
        {
            Type typeT = typeof(T);

            var finalObject = (T)Activator.CreateInstance(typeT, new object[] { });

            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                SetPropertyValue(finalObject, dr.Table.Columns[i].ColumnName, dr[i], typeMapping);
            }

            return(finalObject);
        }
Example #2
0
        /// <summary>
        /// Create and object and fill it's mapped properties through a DataReader record
        /// </summary>
        /// <param name="dr">DataReader in the current record with fields mapped to the object type</param>
        /// <returns>A new instance of the object type with the filled properties</returns>
        private T CreateObject <T>(IDataReader dr, TarkTypeMapping <T> typeMapping, string[] columnNames)
        {
            var finalObject = (T)Activator.CreateInstance(typeof(T));

            object[] values = new object[dr.FieldCount];
            dr.GetValues(values);

            for (int i = 0; i < dr.FieldCount; i++)
            {
                SetPropertyValue(finalObject, columnNames[i], values[i], typeMapping);
            }

            return(finalObject);
        }
Example #3
0
        /// <summary>
        /// Fill an object property according it's mapped column name
        /// </summary>
        /// <param name="obj">object to receive the value</param>
        /// <param name="columnName">name of the column mapped to the property</param>
        /// <param name="value">value to be filled in the property</param>
        /// <exception cref="ArgumentNullException"></exception>
        private void SetPropertyValue <T>(T obj, string columnName, object value, TarkTypeMapping <T> typeMapping)
        {
            TarkColumnMapping columnMapping;

            if (!typeMapping.GetPropertiesMapping().TryGetValue(columnName, out columnMapping))
            {
                return;
            }
            //2017-05-25: Changed to don't throw exceptions if there is no property for the field
            //throw new MissingMemberException(String.Format("Cannot find mapped property for column \"{0}\"",columnName));

            if (value == DBNull.Value)
            {
                return;
            }

            columnMapping.Property.SetValue(obj,
                                            Convert.ChangeType(value,
                                                               columnMapping.GetCachePropertyConvertType(),
                                                               CultureInfo.InvariantCulture
                                                               )
                                            );
        }