internal static void GetRequiredPropertiesCollection(Type modelType, out HashSet <string> requiredProperties, out HashSet <string> skipProperties) { requiredProperties = new HashSet <string>(StringComparer.OrdinalIgnoreCase); skipProperties = new HashSet <string>(StringComparer.OrdinalIgnoreCase); // Use attributes on the property before attributes on the type. ICustomTypeDescriptor modelDescriptor = TypeDescriptorHelper.Get(modelType); PropertyDescriptorCollection propertyDescriptors = modelDescriptor.GetProperties(); BindingBehaviorAttribute typeAttr = modelDescriptor.GetAttributes().OfType <BindingBehaviorAttribute>().SingleOrDefault(); foreach (PropertyDescriptor propertyDescriptor in propertyDescriptors) { BindingBehaviorAttribute propAttr = propertyDescriptor.Attributes.OfType <BindingBehaviorAttribute>().SingleOrDefault(); BindingBehaviorAttribute workingAttr = propAttr ?? typeAttr; if (workingAttr != null) { switch (workingAttr.Behavior) { case BindingBehavior.Required: requiredProperties.Add(propertyDescriptor.Name); break; case BindingBehavior.Never: skipProperties.Add(propertyDescriptor.Name); break; } } } }
public void Behavior_Property() { // Arrange BindingBehavior expectedBehavior = (BindingBehavior)(-20); // Act BindingBehaviorAttribute attr = new BindingBehaviorAttribute(expectedBehavior); // Assert Assert.AreEqual(expectedBehavior, attr.Behavior); }