/// <summary> /// Create a parameter from FieldInfo (Field) /// </summary> /// <param name="field"></param> /// <returns></returns> public static TypedParameter Create(FieldInfo field) { var attr = field.GetCustomAttribute <ParameterAttribute>(); var name = attr.Name ?? field.Name; var type = field.FieldType; var(lower, upper, factor) = Parse(attr, type); IReadOnlyList <object> values; if (type == typeof(int)) { values = new Int32ArithmeticProgression(Convert.ToInt32(attr.Lower), Convert.ToInt32(attr.Upper), Convert.ToInt32(attr.Step)); } else if (type == typeof(double)) { if (attr.Lower == null) { attr.Lower = short.MinValue; } if (attr.Upper == null) { attr.Upper = short.MaxValue; } if (attr.Step == null) { attr.Step = 1; } values = new DoubleArithmeticProgression(Convert.ToDouble(attr.Lower), Convert.ToDouble(attr.Upper), Convert.ToDouble(attr.Step)); } else { values = null; } var domain = new Domain { Upper = upper, Lower = lower, SizeFactor = factor }; return(new TypedParameter { Name = name, Type = type, Domain = domain, Getter = field.GetValue, Setter = field.SetValue, Values = values }); }
/// <summary> /// Create a parameter from PropertyInfo (Property) /// </summary> /// <param name="property"></param> /// <returns></returns> public static TypedParameter Create(PropertyInfo property) { var attr = property.GetCustomAttribute <ParameterAttribute>(); if (!property.CanWrite || !property.CanRead) { return(null); } var name = attr.Name ?? property.Name; var type = property.PropertyType; var(lower, upper, factor) = Parse(attr, type); IReadOnlyList <object> values; if (type == typeof(int)) { values = new Int32ArithmeticProgression(Convert.ToInt32(attr.Lower), Convert.ToInt32(attr.Upper), Convert.ToInt32(attr.Step)); } else if (type == typeof(double)) { values = new DoubleArithmeticProgression(Convert.ToDouble(attr.Lower), Convert.ToDouble(attr.Upper), Convert.ToDouble(attr.Step)); } else { values = null; } var domain = new Domain() { Description = attr.Domain, Lower = lower, Upper = upper, SizeFactor = factor }; return(new TypedParameter { Name = name, Type = type, Domain = domain, Priority = attr.Priority, Getter = property.GetValue, Setter = property.SetValue, Values = values }); }