object MapRecurringField(Action action, Type modelType, object value, Field f) { modelType = modelType.GetGenericArguments().FirstOrDefault(); var v = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(modelType)); dynamic t; if (value is IEnumerable <string> ) { t = JsonConvert.DeserializeObject <dynamic[][]>((value as IEnumerable <string>).ElementAt(0)); } else { t = JsonConvert.DeserializeObject <dynamic[][]>(value.ToString()); } if (t == null) { return(null); } foreach (var item in t) { RecurringFieldModel model = Activator.CreateInstance(modelType) as RecurringFieldModel; foreach (var field in item) { var fField = RecurringFieldModel.Fields(modelType, null).FirstOrDefault(s => $"{f.UniqueName}_{s.UniqueName}" == field.name.Value); if (fField == null) { fField = RecurringFieldModel.Fields(modelType, null).FirstOrDefault(s => s.UniqueName == field.name.Value); } var mapped = Map(action, fField, fField.PropertyType, field.value.ToString()); fField.PropertyInfo.SetValue(model, mapped); } v.Add(model); } return(v); }
public object Map(Action action, Field field, Type type, object value) { if (value == null || value.ToString() == "") { return(null); } if (type == typeof(string)) { return(MapString(value)); } else if (type == typeof(int)) { return(MapInt(value)); } else if (type == typeof(decimal)) { return(MapDecimal(value)); } else if (typeof(IEnumerable <bool?>).IsAssignableFrom(type)) { return(MapChecklist(value)); } else if (field.FieldAttribute is CheckboxFieldAttribute) { return(MapBool(value)); } else if (type == typeof(bool)) { return(MapBool(value)); } else if (type == typeof(DateTime)) { return(MapDateTime(value)); } else if (type == typeof(TimeSpan)) { return(MapTimeSpan(value)); } else if (type.IsEnum) { return(MapEnum(type, value)); } else if (type == typeof(Attachment) && field.FieldAttribute is FileUploadFieldAttribute) { if (value.ToString().Length < 100) { return(MapUploadedFile(value, field)); } else { return(MapFileAsset(value)); } } else if (field.FieldAttribute is BulkFileUploadFieldAttribute) { return(MapUnusedExistingFileAsset(value)); } else if (typeof(IEnumerable <Entity>).IsAssignableFrom(type)) { return(MapEntityArray(type, value)); } else if (field.FieldAttribute.Provider != null && !field.FieldAttribute.DisableProviderReturnValue) { return(MapProvided(action, field, type, value)); } else if (type == typeof(PhoneNumber.PhoneNumberViewModel)) { return(MapPhoneNumberViewModel(value)); } else if (typeof(Entity).IsAssignableFrom(type)) { return(MapEntity(type, value)); } else if (type == typeof(HttpPostedFileWrapper)) { return(value); } else if (typeof(IEnumerable <RecurringFieldModel>).IsAssignableFrom(type)) { return(MapRecurringField(action, type, value, field)); } else if (typeof(ICustomViewModel).IsAssignableFrom(type)) { return(MapCustomViewModel(type, value)); } else { throw new NotImplementedException($"Can't map {type.FullName}, you need to add a custom mapping."); } }
object MapProvided(Action action, Field field, Type type, object value) { return(field.FieldAttribute.GetProvider().Single(action, action.User, value.ToString())); }