< Summary

Information
Class: Fluorite.Strainer.Services.Metadata.PropertyInfoProvider
Assembly: Fluorite.Strainer
File(s): /builds/fluorite/strainer/src/Strainer/Services/Metadata/PropertyInfoProvider.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 71
Line coverage: 100%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
GetPropertyInfo(...)100%11100%
GetPropertyInfoAndFullName(...)90%1010100%
GetPropertyInfos(...)100%11100%

File(s)

/builds/fluorite/strainer/src/Strainer/Services/Metadata/PropertyInfoProvider.cs

#LineLine coverage
 1using System.Linq.Expressions;
 2using System.Reflection;
 3
 4namespace Fluorite.Strainer.Services.Metadata;
 5
 6/// <summary>
 7/// Provides <see cref="PropertyInfo"/> and full member name when supplied
 8/// with an <see cref="Expression{TDelegate}"/>.
 9/// </summary>
 10public class PropertyInfoProvider : IPropertyInfoProvider
 11{
 2412    private readonly BindingFlags _bindingFlags = BindingFlags.Instance | BindingFlags.Public;
 13
 14    public PropertyInfo GetPropertyInfo(Type type, string name)
 15    {
 116        Guard.Against.Null(type);
 117        Guard.Against.NullOrWhiteSpace(name);
 18
 119        return type.GetProperty(name, _bindingFlags);
 20    }
 21
 22    /// <inheritdoc/>
 23    /// <exception cref="ArgumentNullException">
 24    /// <paramref name="expression"/> is <see langword="null"/>.
 25    /// </exception>
 26    /// <exception cref="ArgumentException">
 27    /// <paramref name="expression"/> is invalid expression,
 28    /// not leading to a readable property.
 29    /// </exception>
 30    public (PropertyInfo PropertyInfo, string FullName) GetPropertyInfoAndFullName<T>(Expression<Func<T, object>> expres
 31    {
 1032        Guard.Against.Null(expression);
 33
 34        MemberExpression? body;
 1035        if (expression.Body is MemberExpression)
 36        {
 637            body = expression.Body as MemberExpression;
 38        }
 39        else
 40        {
 441            var ubody = expression.Body as UnaryExpression;
 442            body = ubody?.Operand as MemberExpression;
 43        }
 44
 1045        if (body?.Member is not PropertyInfo propertyInfo)
 46        {
 247            throw new ArgumentException(
 248                $"Expression for '{nameof(T)}' {expression} " +
 249                $"is not a valid expression leading to a readable property.");
 50        }
 51
 852        var stack = new Stack<string>();
 53
 1654        while (body != null)
 55        {
 856            stack.Push(body.Member.Name);
 857            body = body.Expression as MemberExpression;
 58        }
 59
 860        var fullName = string.Join(".", stack.ToArray());
 61
 862        return (propertyInfo, fullName);
 63    }
 64
 65    public PropertyInfo[] GetPropertyInfos(Type type)
 66    {
 167        Guard.Against.Null(type);
 68
 169        return type.GetProperties(_bindingFlags);
 70    }
 71}