< Summary

Information
Class: MyNet.Reflection.PropertyAccessorCache<T>
Assembly: MyNet.Reflection
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Reflection/PropertyAccessorCache.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 6
Uncovered lines: 0
Coverable lines: 6
Total lines: 46
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Get(...)100%22100%
Create(...)100%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Reflection/PropertyAccessorCache.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="PropertyAccessorCache.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Concurrent;
 9using System.Linq;
 10using System.Linq.Expressions;
 11using MyNet.Primitives;
 12
 13namespace MyNet.Reflection;
 14
 15/// <summary>
 16/// Caches compiled property accessors for sorting properties to improve performance when sorting by property names.
 17/// </summary>
 18/// <typeparam name="T">Type of object.</typeparam>
 19public static class PropertyAccessorCache<T>
 20{
 321    private static readonly ConcurrentDictionary<string, Func<T, object?>> Cache = new();
 22
 23    /// <summary>
 24    /// Gets a compiled property accessor for the specified property name, using a cache to improve performance. If the 
 25    /// </summary>
 26    /// <param name="propertyName">The name of the property for which to get the accessor.</param>
 27    /// <returns>A function that accesses the specified property.</returns>
 1528    public static Func<T, object?> Get(string propertyName) => Cache.GetOrAdd(propertyName, Create);
 29
 30    /// <summary>
 31    /// Creates a compiled property accessor for the specified property name, using expression trees to generate a funct
 32    /// </summary>
 33    /// <param name="propertyName">The name of the property for which to create the accessor.</param>
 34    /// <returns>A function that accesses the specified property.</returns>
 35    private static Func<T, object?> Create(string propertyName)
 36    {
 937        var param = Expression.Parameter(typeof(T), "x");
 38
 939        var body = propertyName.Split('.').Aggregate<string?, Expression>(param, (current, member) => Expression.Propert
 40
 641        var convert = Expression.Convert(body, typeof(object));
 42
 643        return Expression.Lambda<Func<T, object?>>(convert, param).Compile();
 44    }
 45}
 46