private void FillProperties(BaseParams newObject, IList <string> rowValues, IList <string> propertyOrder) { var properties = newObject.GetPropertiesToExport(); foreach (PropertyInfo property in properties.Where(p => p.CanWrite)) { ExportableAttribute attribute = property.GetCustomAttribute <ExportableAttribute>(); int index = propertyOrder.IndexOf(attribute.ElementName); string value = string.Empty; if (index < rowValues.Count) { value = rowValues[index]; } try { property.SetValue(newObject, Convert.ChangeType(value, property.PropertyType, ImportExportSvc.USCulture), null); } catch (FormatException exception) { newObject.AddValidationError(property.Name, $"Exception when parsing value from file: {exception.Message}"); } } }
/// <summary> /// Creates header - property names /// </summary> /// <param name="transactionParams"></param> /// <param name="includeReferenceNumber"></param> /// <returns></returns> public string GetHeader(BaseParams transactionParams, bool includeReferenceNumber) { IList <string> names = transactionParams.GetPropertiesToExport() .Select(x => x.GetCustomAttribute <XmlElementAttribute>().ElementName).ToList(); if (includeReferenceNumber) { names.Insert(0, "Result"); } return($"{string.Join(",", names)}"); }
private BaseParams MapParams(BaseParams transactionParams) { var mapped = transactionParams.Clone(); foreach (PropertyInfo propertyInfo in transactionParams.GetPropertiesToExport()) { var attribute = propertyInfo.GetCustomAttribute <ExportableAttribute>(); if (attribute?.ExportValueMap != null) { var mappedValue = GetDisplayedValue(propertyInfo.GetValue(mapped) as string, attribute.ExportValueMap); propertyInfo.SetValue(mapped, mappedValue); } } return(mapped); }
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue) { base.OnItemsSourceChanged(oldValue, newValue); foreach (var column in _generatedColumns) { Columns.Remove(column); } _generatedColumns.Clear(); IList <BaseParams> list = (IList <BaseParams>)newValue; BaseParams firstItem = list.FirstOrDefault(); if (firstItem != null) { var properties = firstItem.GetPropertiesToExport(); foreach (PropertyInfo property in properties) { var attribute = property.GetCustomAttribute <ExportableAttribute>(); DataGridColumn column = null; if (property.PropertyType == typeof(string) || property.PropertyType == typeof(decimal) || property.PropertyType == typeof(bool)) { column = new DataGridTextColumn { Header = attribute.ColumnHeader, Binding = new Binding(property.Name) { ValidatesOnDataErrors = true }, ElementStyle = (Style)FindResource("errorStyle") }; } if (column != null) { _generatedColumns.Add(column); Columns.Add(column); } } } }
private void Validate(BaseParams transaction) { foreach (PropertyInfo propertyInfo in transaction.GetPropertiesToExport()) { string propertyName = propertyInfo.Name; object propertyValue = propertyInfo.GetValue(transaction); var attribute = propertyInfo.GetCustomAttribute <ExportableAttribute>(); string errorMessage = null; if (attribute.IsRequired && propertyInfo.PropertyType == typeof(string) && string.IsNullOrWhiteSpace((string)propertyValue)) { errorMessage = "Value is required"; } if (errorMessage == null) { switch (propertyName) { case nameof(BaseParams.TransactionName): errorMessage = ValidateTransactionName((string)propertyValue); break; case nameof(BaseParams.Environment): errorMessage = ValidateEnvironment((string)propertyValue); break; case nameof(BaseParams.AgentCountryIsoCode): errorMessage = ValidateCountry((string)propertyValue); break; case nameof(BaseParams.AgentState): errorMessage = ValidateAgentState((string)propertyValue); break; case nameof(BaseParams.AmtRange): errorMessage = ValidateAmountRange((string)propertyValue); break; case nameof(BaseParams.CustomAmount): //currently no specific validation break; case nameof(BaseParams.ThirdPartyType): errorMessage = ValidateThirdPartyType((string)propertyValue); break; case nameof(SendParameters.Country): errorMessage = ValidateCountry((string)propertyValue); break; case nameof(SendParameters.State): errorMessage = ValidateState((string)propertyValue); break; case nameof(SendParameters.FeeType): errorMessage = ValidateFeeType((string)propertyValue); break; case nameof(SendParameters.SendCurr): errorMessage = ValidateCurrency((string)propertyValue); break; case nameof(SendParameters.ServiceOption): errorMessage = ValidateServiceOption((string)propertyValue); break; case nameof(SendReversalParameters.RefundReason): errorMessage = ValidateRefundReason((string)propertyValue); break; case nameof(BillPayParameters.BillerCode): errorMessage = ValidateBillerCode((string)propertyValue); break; case nameof(BillPayParameters.BillerAccountNumber): errorMessage = ValidateBillerAccountNumber((string)propertyValue); break; } } if (errorMessage != null) { transaction.AddValidationError(propertyName, errorMessage); } } transaction.IsEnabled = transaction.IsValid; }