Last active
September 22, 2016 08:10
-
-
Save cihanyakar/44b582987efa6328a0b5a652051ba24c to your computer and use it in GitHub Desktop.
CustomObjectReader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ObjectDataReader<T> : IDataReader | |
{ | |
private readonly IList<PropertyInfo> _properties; | |
private readonly IEnumerator _enumarator; | |
public ObjectDataReader(IEnumerable<T> data) | |
{ | |
_enumarator = data.GetEnumerator(); | |
Depth = 0; | |
IsClosed = false; | |
_properties = typeof(T).GetProperties().ToList(); | |
FieldCount = _properties.Count; | |
RecordsAffected = 0; | |
} | |
private object Current => _enumarator.Current; | |
public void Close() | |
{ | |
} | |
public DataTable GetSchemaTable() | |
{ | |
throw new NotImplementedException(); | |
} | |
public bool GetBoolean(int ordinal) | |
{ | |
return (bool)GetValue(ordinal); | |
} | |
public byte GetByte(int ordinal) | |
{ | |
return (byte)GetValue(ordinal); | |
} | |
public long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length) | |
{ | |
throw new NotImplementedException(); | |
} | |
public char GetChar(int ordinal) | |
{ | |
return (char)GetValue(ordinal); | |
} | |
public long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length) | |
{ | |
throw new NotImplementedException(); | |
} | |
public string GetDataTypeName(int ordinal) | |
{ | |
return GetValue(ordinal).GetType().Name; | |
} | |
public DateTime GetDateTime(int ordinal) | |
{ | |
return (DateTime)GetValue(ordinal); | |
} | |
public IDataReader GetData(int i) | |
{ | |
throw new NotImplementedException(); | |
} | |
public decimal GetDecimal(int ordinal) | |
{ | |
return (decimal)GetValue(ordinal); | |
} | |
public double GetDouble(int ordinal) | |
{ | |
return (double)GetValue(ordinal); | |
} | |
public IEnumerator GetEnumerator() | |
{ | |
return _enumarator; | |
} | |
public bool NextResult() | |
{ | |
return _enumarator.MoveNext(); | |
} | |
public bool Read() | |
{ | |
return _enumarator.MoveNext(); | |
} | |
public int Depth { get; } | |
public bool IsClosed { get; } | |
public int RecordsAffected { get; } | |
public object this[string name] | |
{ | |
get | |
{ | |
return _properties.First(x => x.Name == name).GetValue(Current); | |
} | |
} | |
public object this[int ordinal] => GetValue(ordinal); | |
public int FieldCount { get; } | |
public bool HasRows { get; } | |
public bool IsDBNull(int ordinal) | |
{ | |
return GetValue(ordinal) == null; | |
} | |
public int GetValues(object[] values) | |
{ | |
throw new NotImplementedException(); | |
} | |
public object GetValue(int ordinal) | |
{ | |
var property = _properties[ordinal]; | |
return property.GetValue(Current); | |
} | |
public string GetString(int ordinal) | |
{ | |
return (string)GetValue(ordinal); | |
} | |
public int GetOrdinal(string name) | |
{ | |
return _properties.IndexOf(_properties.First(x => x.Name == name)); | |
} | |
public string GetName(int ordinal) | |
{ | |
return _properties[ordinal].Name; | |
} | |
public long GetInt64(int ordinal) | |
{ | |
return (long)_properties[ordinal].GetValue(Current); | |
} | |
public int GetInt32(int ordinal) | |
{ | |
return (int)_properties[ordinal].GetValue(Current); | |
} | |
public short GetInt16(int ordinal) | |
{ | |
return (short)_properties[ordinal].GetValue(Current); | |
} | |
public Guid GetGuid(int ordinal) | |
{ | |
return (Guid)_properties[ordinal].GetValue(Current); | |
} | |
public float GetFloat(int ordinal) | |
{ | |
return (float)_properties[ordinal].GetValue(Current); | |
} | |
public Type GetFieldType(int ordinal) | |
{ | |
return _properties[ordinal].PropertyType; | |
} | |
public void Dispose() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment