Esempio n. 1
0
        /// <summary>
        /// Writes contents of datatable to a delimited stringwriter
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="delimiter"></param>
        /// <returns></returns>
        public static StringWriter Delimit(this DataTable dt, string delimiter)
        {
            // Vars
            StringWriter sw;

            // Write table contents into string
            using (sw = new StringWriter())
            {
                // Write out header
                var headerColumns = new string[dt.Columns.Count];
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    headerColumns[i] = dt.Columns[i].ColumnName;
                }
                sw.WriteLine(String.Join(delimiter, headerColumns));

                // Write out each row
                foreach (DataRow row in dt.Rows)
                {
                    var rowValues = new string[dt.Columns.Count];
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        rowValues[i] = (string)EnhancedConvertor.ChangeType(row[i], typeof(string));
                    }
                    sw.WriteLine(String.Join(delimiter, rowValues));
                }
            }

            return(sw);
        }
Esempio n. 2
0
        /// <summary>
        /// Convenient wrapper around <see cref="DataRowExtensions.Field{T}(DataRow,DataColumn)"/>
        /// to allow supplying an Enum to define the column index
        /// </summary>
        /// <remarks>
        /// This method uses the <see cref="EnhancedConvertor"/> to provide better support for casting between
        /// the underlying field value and the desired type <typeparamref name="T"/>
        /// </remarks>
        public static T Field <T>(this DataRow source, Enum columnIndex)
        {
            var column   = (int)Convert.ChangeType(columnIndex, typeof(int));
            var rawValue = source.Field <object>(column);

            return((T)EnhancedConvertor.ChangeType(rawValue, typeof(T)));
        }
Esempio n. 3
0
        /// <summary>
        /// Convenient wrapper around <see cref="System.Data.DataRowExtensions.Field{T}(DataRow,DataColumn)"/>
        /// to allow supplying an Enum to define the column index and a default value (<paramref name="valueIfNull"/>)
        /// for when the column value returns a <see cref="DBNull"/>
        /// </summary>
        /// <remarks>
        /// This method uses the <see cref="EnhancedConvertor"/> to provide better support for casting between
        /// the underlying field value and the desired type <typeparamref name="T"/>
        /// </remarks>
        public static T Field <T>(this DataRow source, Enum columnIndex, T valueIfNull)
        {
            var column = (int)Convert.ChangeType(columnIndex, typeof(int));

            return((T)(Convert.IsDBNull(source[column])
                            ? valueIfNull
                            : EnhancedConvertor.ChangeType(source[column], typeof(T))));
        }
        public override object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            var valueFromDb = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);

            if (string.IsNullOrEmpty(valueFromDb))
            {
                return(default(T));
            }

            string concatenatedEnumName = valueFromDb.Replace(" ", "");

            return(EnhancedConvertor.ChangeType(concatenatedEnumName, typeof(T)));
        }
 string IStringSerializer.Serialize <T>(T o)
 {
     return(Serialize(EnhancedConvertor.ChangeType <TSubject>(o)));
 }
 T IStringSerializer.Deserialize <T>(string encryptedText)
 {
     return(EnhancedConvertor.ChangeType <T>(Deserialize(encryptedText)));
 }