| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="PropertyDependencyExtractor.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Linq.Expressions; |
| | | 10 | | using System.Reflection; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Observable.Collections; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Extracts the names of properties accessed in filter and sort expressions for items of type T. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <typeparam name="T">The type of items managed by the extended collection.</typeparam> |
| | | 18 | | public sealed class PropertyDependencyExtractor<T> |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Extracts the names of properties accessed in the given expression by traversing the expression tree and collecti |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="expression">The expression to analyze.</param> |
| | | 24 | | /// <returns>A set of property names accessed in the expression.</returns> |
| | | 25 | | private static HashSet<string> Extract(Expression expression) |
| | | 26 | | { |
| | 60 | 27 | | var set = new HashSet<string>(); |
| | 60 | 28 | | Visit(expression, set); |
| | 60 | 29 | | return set; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Recursively visits the given expression and its sub-expressions to extract property names. The method handles di |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="expr">The expression to visit.</param> |
| | | 36 | | /// <param name="set">The set to which extracted property names are added.</param> |
| | | 37 | | private static void Visit(Expression expr, HashSet<string> set) |
| | | 38 | | { |
| | | 39 | | while (true) |
| | | 40 | | { |
| | | 41 | | switch (expr) |
| | | 42 | | { |
| | | 43 | | case MemberExpression { Member.MemberType: MemberTypes.Property } m: |
| | 24 | 44 | | set.Add(m.Member.Name); |
| | 24 | 45 | | expr = m.Expression!; |
| | 24 | 46 | | continue; |
| | | 47 | | |
| | | 48 | | case BinaryExpression b: |
| | 60 | 49 | | Visit(b.Left, set); |
| | 60 | 50 | | expr = b.Right; |
| | 60 | 51 | | continue; |
| | | 52 | | |
| | | 53 | | case UnaryExpression u: |
| | 15 | 54 | | expr = u.Operand; |
| | 15 | 55 | | continue; |
| | | 56 | | |
| | | 57 | | case MethodCallExpression c: |
| | 63 | 58 | | foreach (var a in c.Arguments) Visit(a, set); |
| | | 59 | | break; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | break; |
| | | 63 | | } |
| | 9 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Extracts the names of properties accessed in the given filter expression by analyzing its expression tree. The m |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="expression">The filter expression to analyze.</param> |
| | | 70 | | /// <returns>A set of property names accessed in the filter expression.</returns> |
| | 45 | 71 | | public IReadOnlySet<string> ExtractFilter(Expression<Func<T, bool>> expression) => Extract(expression.Body); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Extracts the names of properties accessed in the given sort expression by analyzing its expression tree. The met |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="expression">The sort expression to analyze.</param> |
| | | 77 | | /// <returns>A set of property names accessed in the sort expression.</returns> |
| | 15 | 78 | | public IReadOnlySet<string> ExtractSort(Expression<Func<T, object?>> expression) => Extract(expression.Body); |
| | | 79 | | } |
| | | 80 | | |