public static void AssignValue <T>(this TypeAccessor accessor, T t, MemberSet members, string fieldName, object fieldValue) { var index = fieldName.IndexOf('.'); if (index == -1) { if (members.Any(m => string.Equals(m.Name, fieldName, StringComparison.OrdinalIgnoreCase))) { accessor[t, fieldName] = fieldValue; } } else { string fieldNameNested = fieldName.Substring(0, index); var member = members.FirstOrDefault(m => string.Equals(m.Name, fieldNameNested, StringComparison.OrdinalIgnoreCase)); if (member != null) { var nestedAccesor = TypeAccessor.Create(member.Type); var tNested = accessor[t, fieldNameNested]; if (tNested == null) { tNested = Activator.CreateInstance(member.Type); accessor[t, fieldNameNested] = tNested; } nestedAccesor.AssignValue(tNested, nestedAccesor.GetMembers(), fieldName.Substring(index + 1), fieldValue); } } }
// Returns a reflected object list results of a supplied type from a given query command protected static List <object> ProcessQuery(string db, Type type, string command) { List <object> objList = new List <object>(); SQLiteConnection conn = new SQLiteConnection(DB_PATH + db + ";"); try { conn.Open(); SQLiteCommand cmd = new SQLiteCommand(conn) { CommandText = command }; SQLiteDataReader rdr = cmd.ExecuteReader(); TypeAccessor ta_dest = GetTypeAccessor(type); MemberSet ta_members = ta_dest.GetMembers(); string fieldname; while (rdr.Read()) { object instance = Activator.CreateInstance(type); for (int i = 0; i < rdr.FieldCount; i++) { fieldname = rdr.GetName(i); if (!rdr.IsDBNull(i) && ta_members.Any(m => string.Equals(m.Name, fieldname, StringComparison.OrdinalIgnoreCase))) { var fvalue = rdr.GetValue(i); ta_dest[instance, fieldname] = fvalue; } } objList.Add(instance); } rdr.Close(); CloseConnection(conn); return(objList); } catch (Exception e) { Console.WriteLine(e); throw new Exception(e.Message); } }