< Summary

Information
Class: MyNet.Observable.Behaviors.PropertyValueAccess
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Behaviors/PropertyValueAccess.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 10
Uncovered lines: 0
Coverable lines: 10
Total lines: 41
Line coverage: 100%
Branch coverage
70%
Covered branches: 7
Total branches: 10
Branch coverage: 70%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryGetValue(...)100%11100%
IsBenignGetterFailure(...)70%1010100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Behaviors/PropertyValueAccess.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="PropertyValueAccess.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Reflection;
 9
 10namespace MyNet.Observable.Behaviors;
 11
 12/// <summary>
 13/// Safe property reads for behavior initialization while derived constructors may still be running.
 14/// </summary>
 15internal static class PropertyValueAccess
 16{
 17    /// <summary>
 18    /// Tries to read a property value without surfacing exceptions from getters that depend on uninitialized state.
 19    /// </summary>
 20    public static bool TryGetValue(object target, PropertyInfo property, out object? value)
 21    {
 99622        ArgumentNullException.ThrowIfNull(target);
 99623        ArgumentNullException.ThrowIfNull(property);
 24
 25        try
 26        {
 99627            value = property.GetValue(target);
 87028            return true;
 29        }
 12630        catch (Exception ex) when (IsBenignGetterFailure(ex))
 31        {
 12632            value = null;
 12633            return false;
 34        }
 99635    }
 36
 37    private static bool IsBenignGetterFailure(Exception exception)
 25238        => exception is NullReferenceException or InvalidOperationException
 25239            || (exception is TargetInvocationException { InnerException: { } inner } && IsBenignGetterFailure(inner));
 40}
 41