< Summary

Information
Class: MyNet.Observable.Collections.PropertyDependencyExtractor<T>
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/PropertyDependencyExtractor.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 15
Uncovered lines: 0
Coverable lines: 15
Total lines: 80
Line coverage: 100%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Extract(...)100%11100%
Visit(...)92.85%1414100%
ExtractFilter(...)100%11100%
ExtractSort(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/PropertyDependencyExtractor.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="PropertyDependencyExtractor.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Linq.Expressions;
 10using System.Reflection;
 11
 12namespace 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>
 18public 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    {
 6027        var set = new HashSet<string>();
 6028        Visit(expression, set);
 6029        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:
 2444                    set.Add(m.Member.Name);
 2445                    expr = m.Expression!;
 2446                    continue;
 47
 48                case BinaryExpression b:
 6049                    Visit(b.Left, set);
 6050                    expr = b.Right;
 6051                    continue;
 52
 53                case UnaryExpression u:
 1554                    expr = u.Operand;
 1555                    continue;
 56
 57                case MethodCallExpression c:
 6358                    foreach (var a in c.Arguments) Visit(a, set);
 59                    break;
 60            }
 61
 62            break;
 63        }
 964    }
 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>
 4571    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>
 1578    public IReadOnlySet<string> ExtractSort(Expression<Func<T, object?>> expression) => Extract(expression.Body);
 79}
 80