public void Test_Reading_Property_Without_Attributes() { // ARRANGE const string propertyName = nameof(MyTestClass.PropertyWithoutAttributes); MyTestClass testObj = new MyTestClass(); Type type = testObj.GetType(); PropertyDescriptorCollection propertyDescriptors = TypeDescriptor.GetProperties(type); PropertyDescriptor propertyDescriptor = propertyDescriptors[propertyName]; // ACT PropertyAttributeSummary attributes = propertyDescriptor.GetAttributes(); // ASSERT Assert.IsNotNull(attributes); Assert.IsNull(attributes.AuditIgnore); Assert.IsNull(attributes.ForeignKey); Assert.IsNull(attributes.Key); Assert.IsNull(attributes.NotMapped); Assert.IsNull(attributes.Range); }
/// <summary> /// Determines whether [is auditable column] [the specified property descriptors]. /// </summary> /// <param name="propertyDescriptors">Property Descriptors.</param> /// <param name="propertyInfo">Property Information.</param> /// <returns> /// <c>true</c> if [is auditable column] [the specified property descriptors]; otherwise, <c>false</c>. /// </returns> public static bool IsAuditableColumn( PropertyDescriptorCollection propertyDescriptors, PropertyInfo propertyInfo) { if (propertyDescriptors == null) { throw new ArgumentNullException(nameof(propertyDescriptors)); } if (propertyInfo == null) { throw new ArgumentNullException(nameof(propertyInfo)); } // Get property descriptor details for model property PropertyDescriptor propertyDescriptor = propertyDescriptors[propertyInfo.Name]; if (propertyDescriptor == null) { return(false); } PropertyAttributeSummary propertyAttributes = propertyDescriptor.GetAttributes(); // Auditable if // - Has a setter // - Not got [Key] attribute // - Not got [ForeignKey] attribute // - Not got [NotMapped] attribute // - Not got [AuditIgnore] attribute // - Not a collection return(propertyInfo.CanWrite && propertyAttributes.Key == null && propertyAttributes.ForeignKey == null && propertyAttributes.NotMapped == null && propertyAttributes.AuditIgnore == null && !propertyInfo.IsCollection()); }