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}"); } } }
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; }