private static void LoadObjectFromResource(ColumnMappingAttribute attribute, HeaderRecord record) { int index = SearchHeadIndex(attribute, record); if (index != -1) { attribute.getObject = r => { var path = r[index]; if (string.IsNullOrEmpty(path)) { return(null); } var format = attribute.ImportParam as String; if (format != null) { path = string.Format(format, path); } return(Resources.Load(path)); } } ; else { attribute.getObject = r => null; } }
private static int SearchHeadIndex(ColumnMappingAttribute attribute, HeaderRecord record) { var searchs = new List <string>(); if (attribute.m_header.Length == 0) { searchs.Add(attribute.TargetField.Name); } else { searchs.AddRange(attribute.m_header); } var index = -1; var searchCount = searchs.Count; for (int i = 0; i < searchCount; i++) { var search = searchs[i]; if ((index = record.IndexOf(search)) != -1) { return(index); } } return(index); }
public virtual TEntity ToEntity <TEntity>(IDataReader reader) where TEntity : BaseEntity, new() { TEntity newEntity = new TEntity(); try { PropertyInfo[] properties = newEntity.GetType().GetProperties(); int ordinal = 0; object val = new object(); foreach (PropertyInfo property in properties) { ColumnMappingAttribute attribute = property.GetCustomAttribute <ColumnMappingAttribute>(); if (attribute != null) { ordinal = reader.GetOrdinal(attribute.ColumnName); val = reader.GetValue(ordinal); property.SetValue(newEntity, val); } else { ordinal = reader.GetOrdinal(property.Name); val = reader.GetValue(ordinal); property.SetValue(newEntity, val); } } return(newEntity); } catch (Exception) { throw; } }
private static void InitString(ColumnMappingAttribute attribute, HeaderRecord record) { int index = SearchHeadIndex(attribute, record); if (index != -1) { attribute.getObject = r => r[index]; } else { attribute.getObject = r => string.Empty; } }
private static void InitInt32Array(ColumnMappingAttribute attribute, HeaderRecord record) { int index = SearchHeadIndex(attribute, record); if (index != -1) { attribute.getObject = r => { var str = r[index]; var strs = str.Split('_'); var count = strs.Length; if (string.IsNullOrEmpty(str)) { count = 0; } int[] values = new int[count]; int id; for (int i = 0; i < count; i++) { if (string.IsNullOrEmpty(strs[i])) { values[i] = 0; continue; } if (int.TryParse(strs[i], out id)) { values[i] = id; } else { Debug.LogErrorFormat("InitInt32Array int.TryParse(strs[i],out id Failed,strs:{0}:{1}:Count is {2}", str, attribute.TargetField.Name, count); } } return(values); } } ; else { attribute.getObject = r => string.Empty; } }
private static void ConverToInternal <T>(ColumnMappingAttribute attribute, HeaderRecord record) { int index = SearchHeadIndex(attribute, record); var type = typeof(T); if (index != -1) { attribute.getObject = r => { try { return(Convert.ChangeType(r[index], type)); } catch (Exception) { return(default(T)); } } } ; else { attribute.getObject = r => default(T); } }
private static void InitBoolean(ColumnMappingAttribute attribute, HeaderRecord record) { var importP = attribute.ImportParam as string; if (importP != null) { int index = SearchHeadIndex(attribute, record); if (index != -1) { attribute.getObject = r => string.Equals(r[index], importP, StringComparison.InvariantCultureIgnoreCase); } else { attribute.getObject = r => false; } } else { ConverToInternal <bool>(attribute, record); } }
private static void InitGameObject(ColumnMappingAttribute attribute, HeaderRecord record) { LoadObjectFromResource(attribute, record); }
private static void InitSingle(ColumnMappingAttribute attribute, HeaderRecord record) { ConverToInternal <float>(attribute, record); }
private static void InitInt32(ColumnMappingAttribute attribute, HeaderRecord record) { ConverToInternal <int>(attribute, record); }