/// <summary>Sets <c>property</c> to the value of <c>valueAsText</c>.</summary> private static void ValidateAndSetPropertyValue( Command command , PropertyInfo property , CommandParameterType commandParameterType , ref string valueAsText) { if (property.PropertyType.IsAssignableFrom(typeof(string))) { if (!string.IsNullOrWhiteSpace(valueAsText) && commandParameterType == CommandParameterType.InputFile) { FileInfo fi = new FileInfo(valueAsText); Uri absoluteUri = new Uri(fi.FullName, UriKind.Absolute); var referenceUri = new Uri(Environment.CurrentDirectory.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar); var relativeUri = referenceUri.MakeRelativeUri(absoluteUri); valueAsText = Uri.UnescapeDataString(relativeUri.ToString()); if (!File.Exists(valueAsText)) { string errorMessage = string.Format($"Unable to find the file specified for parameter '{property.Name}'."); throw new Exception(errorMessage); } } property.SetValue(command, valueAsText); } else if (property.PropertyType.IsAssignableFrom(typeof(int))) { int value; if (!int.TryParse(valueAsText, out value)) { string errorMessage = string.Format($"The '{property.Name}' parameter is not a valid integer value."); throw new Exception(errorMessage); } if (commandParameterType == CommandParameterType.NonNegativeInteger && value < 0) { string errorMessage = string.Format($"The '{property.Name}' parameter must be a non-negative integer."); throw new Exception(errorMessage); } else if (commandParameterType == CommandParameterType.PositiveInteger && value <= 0) { string errorMessage = string.Format($"The '{property.Name}' parameter must be a positive integer."); throw new Exception(errorMessage); } property.SetValue(command, value); } else if (property.PropertyType.IsAssignableFrom(typeof(double))) { double value; if (!double.TryParse(valueAsText, out value)) { string errorMessage = string.Format($"Parameter '{property.Name}' is not a valid real value."); throw new Exception(errorMessage); } property.SetValue(command, value); } else { throw new Exception($"Field '{property.Name}' has an unanticipated type."); } }
static object ConvertToType(CommandParameterType type, string obj) { switch (type) { case CommandParameterType.StartDate: return(DateTime.TryParse(obj, out DateTime startDate) ? startDate : DateTime.Now); case CommandParameterType.EndDate: return(DateTime.TryParse(obj, out DateTime endDate) ? endDate : DateTime.Now); case CommandParameterType.HoursPerWeek: return(int.TryParse(obj, out int hours) ? hours : 1); default: return(null); } }
public CommandParameterInfo(CommandParameterType commandParameterType, object commandParameterObject) { CommandParameterType = commandParameterType; CommandParameterObject = commandParameterObject; }
public CommandParameter(object data, CommandParameterType type = CommandParameterType.General) { Data = data; Type = type; }