< Summary

Information
Class: Elsa.Extensions.PropertyAccessorExtensions
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Extensions/PropertyAccessorExtensions.cs
Line coverage
41%
Covered lines: 7
Uncovered lines: 10
Coverable lines: 17
Total lines: 78
Line coverage: 41.1%
Branch coverage
10%
Covered branches: 1
Total branches: 10
Branch coverage: 10%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SetPropertyValue(...)100%210%
SetPropertyValue(...)0%620%
GetPropertyValue(...)0%620%
GetPropertyName(...)100%210%
GetProperty(...)16.66%66100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Extensions/PropertyAccessorExtensions.cs

#LineLine coverage
 1using System.Linq.Expressions;
 2using System.Reflection;
 3
 4// ReSharper disable once CheckNamespace
 5namespace Elsa.Extensions;
 6
 7/// <summary>
 8/// Provides extension methods for working with property accessors.
 9/// </summary>
 10public static class PropertyAccessorExtensions
 11{
 12    /// <summary>
 13    /// Sets the value of the property referenced by the expression.
 14    /// </summary>
 15    /// <param name="target">The target on which to set the property value.</param>
 16    /// <param name="propertyName">The name of the property.</param>
 17    /// <param name="value">The value to set.</param>
 18    public static void SetPropertyValue(this object target, string propertyName, object? value)
 19    {
 020        var property = target.GetType().GetProperty(propertyName)!;
 021        property.SetValue(target, value);
 022    }
 23
 24    /// <summary>
 25    /// Sets the value of the property referenced by the expression.
 26    /// </summary>
 27    /// <param name="target">The target on which to set the property value.</param>
 28    /// <param name="expression">The expression.</param>
 29    /// <param name="value">The value to set.</param>
 30    /// <typeparam name="T">The type of the object containing the property.</typeparam>
 31    /// <typeparam name="TProperty">The type of the property.</typeparam>
 32    public static void SetPropertyValue<T, TProperty>(this T target, Expression<Func<T, TProperty>> expression, TPropert
 33    {
 034        var property = expression.GetProperty();
 35
 036        if (property != null)
 037            property.SetValue(target, value, null);
 038    }
 39
 40    /// <summary>
 41    /// Gets the value of the property referenced by the expression.
 42    /// </summary>
 43    /// <param name="target">The target from which to get the property value.</param>
 44    /// <param name="expression">The expression.</param>
 45    /// <typeparam name="T">The type of the object containing the property.</typeparam>
 46    /// <typeparam name="TProperty">The type of the property.</typeparam>
 47    /// <returns>The value of the property referenced by the expression.</returns>
 48    public static TProperty? GetPropertyValue<T, TProperty>(this T target, Expression<Func<T, TProperty>> expression)
 49    {
 050        var property = expression.GetProperty();
 051        return (TProperty?)property?.GetValue(target);
 52    }
 53
 54    /// <summary>
 55    /// Gets the name of the property referenced by the expression.
 56    /// </summary>
 57    /// <param name="expression">The expression.</param>
 58    /// <typeparam name="T">The type of the object containing the property.</typeparam>
 59    /// <typeparam name="TProperty">The type of the property.</typeparam>
 60    /// <returns>The name of the property referenced by the expression.</returns>
 061    public static string GetPropertyName<T, TProperty>(this Expression<Func<T, TProperty>> expression) => expression.Get
 62
 63    /// <summary>
 64    /// Gets the property referenced by the expression.
 65    /// </summary>
 66    /// <param name="expression">The expression.</param>
 67    /// <typeparam name="T">The type of the object containing the property.</typeparam>
 68    /// <typeparam name="TProperty">The type of the property.</typeparam>
 69    /// <returns>The property referenced by the expression.</returns>
 70    public static PropertyInfo? GetProperty<T, TProperty>(this Expression<Func<T, TProperty>> expression) =>
 40471        expression.Body is MemberExpression memberExpression
 40472            ? memberExpression.Member as PropertyInfo
 40473            : expression.Body is UnaryExpression unaryExpression
 40474                ? unaryExpression.Operand is MemberExpression unaryMemberExpression
 40475                    ? unaryMemberExpression.Member as PropertyInfo
 40476                    : null
 40477                : null;
 78}