/// <summary> /// Helper method to convert the value of the argument to a different format, using a default /// value if the conversion fails. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="argument"></param> /// <param name="transform"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static T As <T>(this IValuedArgument argument, Func <string, T> transform, T defaultValue) { if (argument is MissingArgument) { return(defaultValue); } try { return(transform(argument.Value)); } catch { return(default);
private static void AssignPropertyValue <T>(IValuedArgument argument, PropertyInfo property, T obj) { if (!argument.Exists()) { return; } if (property.PropertyType == typeof(string)) { property.SetValue(obj, argument.Value); } else if (property.PropertyType == typeof(int) || property.PropertyType == typeof(int?)) { property.SetValue(obj, argument.AsInt()); } else if (property.PropertyType == typeof(long) || property.PropertyType == typeof(long?)) { property.SetValue(obj, argument.AsLong()); } else if (property.PropertyType == typeof(bool) || property.PropertyType == typeof(bool?)) { property.SetValue(obj, argument.AsBool()); } }