MicroLite.TypeConverters.ObjectTypeConverter.ConvertFromDbValue C# (CSharp) Method

ConvertFromDbValue() public method

Converts value at the specified index in the IDataReader into an instance of the specified type.
thrown if propertyType is null.
public ConvertFromDbValue ( IDataReader reader, int index, Type type ) : object
reader IDataReader The IDataReader containing the results.
index int The index of the record to read from the IDataReader.
type System.Type The type to convert result value to.
return object
        public object ConvertFromDbValue(IDataReader reader, int index, Type type)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (reader.IsDBNull(index))
            {
                return null;
            }

            var value = reader[index];

            if (type.IsValueType && type.IsGenericType)
            {
                ValueType converted = (ValueType)value;

                return converted;
            }
            else
            {
                var converted = Convert.ChangeType(value, type, CultureInfo.InvariantCulture);

                return converted;
            }
        }

Same methods

ObjectTypeConverter::ConvertFromDbValue ( object value, Type type ) : object

Usage Example

            public void AnArgumentNullExceptionShouldBeThrown()
            {
                var typeConverter = new ObjectTypeConverter();

                var exception = Assert.Throws<ArgumentNullException>(
                    () => typeConverter.ConvertFromDbValue(1, null));

                Assert.Equal("type", exception.ParamName);
            }