| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using System.Reflection; |
| | | 3 | | |
| | | 4 | | // ReSharper disable once CheckNamespace |
| | | 5 | | namespace Elsa.Extensions; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides extension methods for working with property accessors. |
| | | 9 | | /// </summary> |
| | | 10 | | public 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 | | { |
| | 0 | 20 | | var property = target.GetType().GetProperty(propertyName)!; |
| | 0 | 21 | | property.SetValue(target, value); |
| | 0 | 22 | | } |
| | | 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 | | { |
| | 0 | 34 | | var property = expression.GetProperty(); |
| | | 35 | | |
| | 0 | 36 | | if (property != null) |
| | 0 | 37 | | property.SetValue(target, value, null); |
| | 0 | 38 | | } |
| | | 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 | | { |
| | 0 | 50 | | var property = expression.GetProperty(); |
| | 0 | 51 | | 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> |
| | 0 | 61 | | 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) => |
| | 404 | 71 | | expression.Body is MemberExpression memberExpression |
| | 404 | 72 | | ? memberExpression.Member as PropertyInfo |
| | 404 | 73 | | : expression.Body is UnaryExpression unaryExpression |
| | 404 | 74 | | ? unaryExpression.Operand is MemberExpression unaryMemberExpression |
| | 404 | 75 | | ? unaryMemberExpression.Member as PropertyInfo |
| | 404 | 76 | | : null |
| | 404 | 77 | | : null; |
| | | 78 | | } |