| | | 1 | | using Plainquire.Filter.Abstractions; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Reflection; |
| | | 7 | | |
| | | 8 | | namespace Plainquire.Sort; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Extension methods for <see cref="Type"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | internal static class TypeExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Returns <c>true</c> when the value of then given type can be <c>null</c>; otherwise <c>false</c>. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="type">The type to check.</param> |
| | | 19 | | /// <exception cref="ArgumentNullException"></exception> |
| | | 20 | | public static bool IsNullable(this Type type) |
| | | 21 | | { |
| | 265 | 22 | | if (type == null) |
| | 0 | 23 | | throw new ArgumentNullException(nameof(type)); |
| | | 24 | | |
| | 265 | 25 | | if (type.IsClass) |
| | 256 | 26 | | return true; |
| | | 27 | | |
| | 9 | 28 | | return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Determines whether the given type is <see cref="EntitySort{TEntity}"/>. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="type">The type.</param> |
| | | 35 | | /// <autogeneratedoc /> |
| | | 36 | | public static bool IsGenericEntitySort(this Type type) |
| | 2088 | 37 | | => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(EntitySort<>); |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets all properties sortable by <see cref="EntitySort"/>. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="type">The type.</param> |
| | | 43 | | /// <autogeneratedoc /> |
| | | 44 | | public static IEnumerable<PropertyInfo> GetSortableProperties(this Type type) |
| | 113 | 45 | | => type.GetProperties().Where(property => property.IsPropertySortable()); |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Determines whether this parameter is visible as (MVC controller action) parameter. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="member">The member.</param> |
| | | 51 | | /// <autogeneratedoc /> |
| | | 52 | | private static bool IsPropertySortable(this PropertyInfo member) |
| | | 53 | | { |
| | 346 | 54 | | var isEnumerable = member.PropertyType != typeof(string) && typeof(IEnumerable).IsAssignableFrom(member.Property |
| | 346 | 55 | | var filterAttribute = member.GetCustomAttribute<FilterAttribute>(); |
| | | 56 | | |
| | 346 | 57 | | return isEnumerable |
| | 346 | 58 | | ? filterAttribute?.Sortable == true |
| | 346 | 59 | | : filterAttribute?.Sortable != false; |
| | | 60 | | } |
| | | 61 | | } |