using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Tango.BL.Entities; namespace Tango.PPC.Shared.SQL { public class RemoteSqlRow { private Func _getFuncKey; private Func _getFuncIndex; public List Values { get; set; } public RemoteSqlRow() { Values = new List(); } public Object Get(String columnName) { return _getFuncKey.Invoke(columnName); } public Object Get(int columnIndex) { return _getFuncIndex.Invoke(columnIndex); } public T Get(String columnName) { var value = _getFuncKey.Invoke(columnName); if (typeof(T) != value.GetType()) { return (T)Convert.ChangeType(value, typeof(T)); } else { return (T)value; } } public T Get(int columnIndex) { var value = _getFuncIndex.Invoke(columnIndex); if (typeof(T) != value.GetType()) { return (T)Convert.ChangeType(value, typeof(T)); } else { return (T)value; } } internal void Init(Func getFuncKey, Func getFuncIndex) { _getFuncKey = getFuncKey; _getFuncIndex = getFuncIndex; } public override string ToString() { return String.Join(", ", Values); } public T Map() where T : class, new() { var obj = Activator.CreateInstance(); Map(obj); return obj; } public void Map(T obj) where T : class { foreach (var prop in typeof(T).GetPropertiesWithAttribute()) { try { var columnName = prop.GetCustomAttribute().Name; var value = Get(columnName).ToStringSafe(); prop.SetValue(obj, Convert.ChangeType(value, prop.PropertyType)); } catch (Exception ex) { Debug.WriteLine(ex); } } } } }