Skip to content

Commit

Permalink
Solve the issue with GetCustomAttributes for PropertyInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink committed Jul 11, 2019
1 parent 75f717a commit c128bc1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 25 deletions.
4 changes: 3 additions & 1 deletion Src/FluentAssertions/Common/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ public static bool IsDecoratedWith<TAttribute>(this Type type, bool inherit = fa
private static IEnumerable<TAttribute> GetCustomAttributes<TAttribute>(MemberInfo type, bool inherit = false)
where TAttribute : Attribute
{
return type.GetCustomAttributes(inherit).OfType<TAttribute>();
// Do not use as extension method here, there is an issue with PropertyInfo and EventInfo
// preventing the inherit option to work.
return CustomAttributeExtensions.GetCustomAttributes(type, inherit).OfType<TAttribute>();
}

private static IEnumerable<TAttribute> GetCustomAttributes<TAttribute>(Type type, bool inherit = false)
Expand Down
26 changes: 2 additions & 24 deletions Src/FluentAssertions/Types/PropertyInfoSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,7 @@ public PropertyInfoSelector ThatAreDecoratedWith<TAttribute>()
public PropertyInfoSelector ThatAreDecoratedWithOrInherit<TAttribute>()
where TAttribute : Attribute
{
// TODO: There seems to be a bug with `GetCustomAttributes` when used as extension method
// compared to when used as a classic static method.

// When used as classic static method access, all tests are working as expected.
selectedProperties = selectedProperties.Where(property =>
CustomAttributeExtensions.GetCustomAttributes(property, true).OfType<TAttribute>().Any());

// But when used as extension method, the results differ.
// Comment the previous line and uncomment this one, now some tests will start failing because
// the inherited attribute is not found.
// selectedProperties = selectedProperties.Where(property => property.GetCustomAttributes(true).OfType<TAttribute>().Any());

selectedProperties = selectedProperties.Where(property => property.IsDecoratedWithOrInherit<TAttribute>());
return this;
}

Expand All @@ -97,18 +86,7 @@ public PropertyInfoSelector ThatAreNotDecoratedWith<TAttribute>()
public PropertyInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
where TAttribute : Attribute
{
// TODO: There seems to be a bug with `GetCustomAttributes` when used as extension method
// compared to when used as a classic static method.

// When used as classic static method access, all tests are working as expected.
selectedProperties = selectedProperties.Where(property =>
!CustomAttributeExtensions.GetCustomAttributes(property, true).OfType<TAttribute>().Any());

// But when used as extension method, the results differ.
// Comment the previous line and uncomment this one, now some tests will start failing because
// the inherited attribute is not found.
// selectedProperties = selectedProperties.Where(property => !property.GetCustomAttributes(true).OfType<TAttribute>().Any());

selectedProperties = selectedProperties.Where(property => !property.IsDecoratedWithOrInherit<TAttribute>());
return this;
}

Expand Down

0 comments on commit c128bc1

Please sign in to comment.