public static Dictionary <string, string> GetFieldMappings(Type type, bool includePrimary = true) { PropertyInfo[] props = type.GetProperties(); Dictionary <string, string> mappings = new Dictionary <string, string>(); bool foundAttribute = false; foreach (PropertyInfo prop in props) { foundAttribute = false; object[] attributes = prop.GetCustomAttributes(false); foreach (object attribute in attributes) { if (attribute.GetType() == typeof(DBFieldMapping)) { DBFieldMapping mapping = (DBFieldMapping)attribute; if (!mapping.primarykey || (includePrimary && mapping.primarykey)) { mappings.Add(prop.Name, mapping.field); } foundAttribute = true; } } if (!foundAttribute) { mappings.Add(prop.Name, prop.Name); } } return(mappings); }
public static PropertyInfo GetPrimaryProperty(Type type) { PropertyInfo[] props = type.GetProperties(); List <string> fieldnames = new List <string>(); foreach (PropertyInfo prop in props) { object[] attributes = prop.GetCustomAttributes(false); foreach (object attribute in attributes) { if (attribute.GetType() == typeof(DBFieldMapping)) { DBFieldMapping mapping = (DBFieldMapping)attribute; if (mapping.primarykey) { return(prop); } } } } return(null); }
public static string GetFieldName(Type type, string propertyName) { PropertyInfo[] props = type.GetProperties(); List <string> fieldnames = new List <string>(); foreach (PropertyInfo prop in props) { object[] attributes = prop.GetCustomAttributes(false); if (prop.Name == propertyName) { foreach (object attribute in attributes) { if (attribute.GetType() == typeof(DBFieldMapping)) { DBFieldMapping mapping = (DBFieldMapping)attribute; return(mapping.field); } } return(prop.Name); } } return(null); }
public static void Map(Type type, IDataReader datareader, object obj) { PropertyInfo[] props = type.GetProperties(); bool foundAttribute = false; foreach (PropertyInfo prop in props) { foundAttribute = false; object[] attributes = prop.GetCustomAttributes(false); foreach (object attribute in attributes) { if (attribute.GetType() == typeof(DBFieldMapping)) { DBFieldMapping mapping = (DBFieldMapping)attribute; prop.SetValue(obj, getValue(datareader, mapping.field, prop.PropertyType), null); foundAttribute = true; } } if (!foundAttribute) { prop.SetValue(obj, getValue(datareader, prop.Name, prop.PropertyType), null); } } }