public static ColorerMatrixDescriptor Get(ColorerMatrixType type, float?value = default) { var methods = typeof(ColorerMatrix).GetMethods(BindingFlags.Public | BindingFlags.Static) .Where(m => m.GetCustomAttributes(typeof(ColorerMatrixAttribute), false).Length != 0).ToArray(); foreach (var m in methods) { var attrs = m.GetCustomAttributes(typeof(ColorerMatrixAttribute), false).OfType <ColorerMatrixAttribute>().ToArray(); foreach (var attr in attrs) { if (attr.Type == type) { if (attr.IsValueRequired && value.HasValue) { if (value.Value < attr.MinimumValue) { throw new ArgumentException(string.Format("Value has to be between {0} and {1}.", attr.MinimumValue, attr.MaximumValue), nameof(value)); } if (value.Value > attr.MaximumValue) { throw new ArgumentException(string.Format("Value has to be between {0} and {1}.", attr.MinimumValue, attr.MaximumValue), nameof(value)); } } return(new ColorerMatrixDescriptor( v => (ColorMatrix)m.Invoke(default, attr.IsValueRequired ? new object[] { v } : default),
public ColorerMatrixAttribute(ColorerMatrixType type, bool isValueRequired, float defaultValue = 0, float minimumValue = 0, float maximumValue = 0) { Type = type; IsValueRequired = isValueRequired; if (defaultValue < minimumValue) { throw new ArgumentException(string.Format("The value of {0} cannot be smaller than the value of {1}.", nameof(defaultValue), nameof(minimumValue)), nameof(defaultValue)); } if (maximumValue < defaultValue) { throw new ArgumentException(string.Format("The value of {0} cannot be smaller than the value of {1}.", nameof(maximumValue), nameof(defaultValue)), nameof(defaultValue)); } DefaultValueValue = defaultValue; MinimumValueValue = minimumValue; MaximumValueValue = maximumValue; }
public ColorerMatrixAttribute(ColorerMatrixType type) : this(type, false) { }