< Summary

Information
Class: MyNet.Observable.ObservableObjectPropertyAccess
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Base/ObservableObjectPropertyAccess.cs
Tag: 323_28699572109
Line coverage
92%
Covered lines: 13
Uncovered lines: 1
Coverable lines: 14
Total lines: 56
Line coverage: 92.8%
Branch coverage
50%
Covered branches: 3
Total branches: 6
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
GetPropertyValue(...)100%11100%
CreateGetter(...)50%6687.5%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Base/ObservableObjectPropertyAccess.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ObservableObjectPropertyAccess.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.Expressions;
 10using System.Reflection;
 11
 12#pragma warning disable IDE0130 // Namespace does not match folder structure
 13namespace MyNet.Observable;
 14#pragma warning restore IDE0130 // Namespace does not match folder structure
 15
 16/// <summary>
 17/// Cached compiled property getters for <see cref="ObservableObject"/> instances (startup / attach paths only).
 18/// </summary>
 19internal static class ObservableObjectPropertyAccess
 20{
 321    private static readonly ConcurrentDictionary<(Type Type, string PropertyName), Func<ObservableObject, object?>> Gett
 22
 23    /// <summary>
 24    /// Gets the current value of a public instance property by name.
 25    /// </summary>
 26    public static object? GetPropertyValue(ObservableObject instance, string propertyName)
 27    {
 4228        ArgumentNullException.ThrowIfNull(instance);
 4229        ArgumentException.ThrowIfNullOrWhiteSpace(propertyName);
 30
 4231        var key = (instance.GetType(), propertyName);
 32
 4233        var getter = GetterCache.GetOrAdd(key, static x => CreateGetter(x.Type, x.PropertyName));
 34
 4235        return getter(instance);
 36    }
 37
 38    private static Func<ObservableObject, object?> CreateGetter(Type type, string propertyName)
 39    {
 1240        var property = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);
 41
 1242        if (property?.CanRead != true || property.GetIndexParameters().Length != 0)
 043            return static _ => UnknownValue.Instance;
 44
 1245        var parameter = Expression.Parameter(typeof(ObservableObject));
 46
 1247        var cast = Expression.Convert(parameter, type);
 48
 1249        var propertyAccess = Expression.Property(cast, property);
 50
 1251        var convert = Expression.Convert(propertyAccess, typeof(object));
 52
 1253        return Expression.Lambda<Func<ObservableObject, object?>>(convert, parameter).Compile();
 54    }
 55}
 56