private bool Validations(Models.AbsArguments obj) { Uri uriResult; if (Uri.TryCreate(obj.LocalAddress, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps)) { obj.LocalAddress = uriResult.ToString(); } else { txtLog.Clear(); txtLog.Text = "Address is not valid!"; return(false); } Type type = obj.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { var argument = ""; var val = property.GetValue(obj, null); if (val == null) { continue; } foreach (object attr in property.GetCustomAttributes(true)) { if (attr as ArgumentAttribute != null) { argument = (attr as ArgumentAttribute).Argument; break; } } if (argument == "") { continue; } if (property.PropertyType == typeof(int)) { if ((int)val < 0) { txtLog.Clear(); txtLog.Text = property.Name + " must be greater than 0"; return(false); } } } return(true); }
private string GetArguments(Models.AbsArguments obj) { var sb = new StringBuilder(); var url = ""; Type type = obj.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { var argument = ""; var val = property.GetValue(obj, null); if (val == null) { continue; } foreach (object attr in property.GetCustomAttributes(true)) { if (attr as ArgumentAttribute != null) { argument = (attr as ArgumentAttribute).Argument; break; } } if (argument == "") { continue; } if (property.Name == "GnuplotFile") { if ((bool)val) { sb.Append(argument + " " + GetFilePath("tsv") + " "); } continue; } if (property.Name == "CsvFile") { if ((bool)val) { sb.Append(argument + " " + GetFilePath("csv") + " "); } continue; } if (argument == "-B") { url = val.ToString(); continue; } if (property.PropertyType == typeof(bool)) { if ((bool)val) { sb.Append(argument + " "); } } else if (property.PropertyType == typeof(int)) { if ((int)val > 0) { sb.Append(argument + " " + val.ToString() + " "); } } else if (property.PropertyType == typeof(List <string>)) { foreach (var l in val as List <string> ) { sb.Append(argument + " " + l + " "); } } else if (property.PropertyType == typeof(Verbosity)) { sb.Append(argument + " " + (int)val + " "); } else if (property.PropertyType == typeof(Protocol)) { sb.Append(argument + " " + val.ToString().Replace("_", ".") + " "); } else { sb.Append(argument + " " + val.ToString() + " "); } } sb.Append(url); return(sb.ToString()); }