DesignTimePropertyAttributes GetDesignTimePropertyAttributes(PropertyInfo propertyInfo) { DesignTimePropertyAttributes result = new DesignTimePropertyAttributes(); PrecisionAttribute numDigitsAttribute = propertyInfo.GetCustomAttribute <PrecisionAttribute>(); if (numDigitsAttribute != null) { result.NumDigits = numDigitsAttribute.NumDecimalPlaces; } DisplayTextAttribute displayTextAttribute = propertyInfo.GetCustomAttribute <DisplayTextAttribute>(); if (displayTextAttribute != null) { result.DisplayText = displayTextAttribute.DisplayText; } DependentPropertyAttribute dependentPropertyAttribute = propertyInfo.GetCustomAttribute <DependentPropertyAttribute>(); if (dependentPropertyAttribute != null) { result.DependentProperty = dependentPropertyAttribute.DependentProperty; } return(result); }
private static void ValidateDependentProperty(DependentPropertyAttribute attrib, PropertyInfo property, NewObjectParameters parameters) { var target = parameters.GetType().GetProperty(attrib.Property.ToString()).GetValue(parameters); if (target.ToString() == attrib.RequiredValue.ToString()) { ValidateRequiredValue(property, parameters, attrib); } }
private static void ValidateRequiredValue(PropertyInfo property, NewObjectParameters parameters, DependentPropertyAttribute attrib = null) { var val = property.GetValue(parameters); var dependentStr = attrib != null ? $" when property '{attrib.Property}' is value '{attrib.RequiredValue}'" : ""; if (string.IsNullOrEmpty(val?.ToString())) { throw new InvalidOperationException($"Property '{property.Name}' requires a value{dependentStr}, however the value was null or empty."); } var list = val as IEnumerable; if (list != null && !(val is string)) { var casted = list.Cast <object>(); if (!casted.Any()) { throw new InvalidOperationException($"Property '{property.Name}' requires a value, however an empty list was specified."); } } }