| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NotifyPropertyChangedSourceSnapshot.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Concurrent; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Linq; |
| | | 11 | | using System.Linq.Expressions; |
| | | 12 | | using System.Reflection; |
| | | 13 | | |
| | | 14 | | namespace MyNet.Observable.Behaviors; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Cached compiled getters used when attaching to a wrapper source (attach path only). |
| | | 18 | | /// </summary> |
| | | 19 | | internal static class NotifyPropertyChangedSourceSnapshot |
| | | 20 | | { |
| | 3 | 21 | | private static readonly ConcurrentDictionary<Type, PropertyGetter[]> GetterCache = new(); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Enumerates public readable instance property values on the source object. |
| | | 25 | | /// </summary> |
| | | 26 | | public static IEnumerable<(string PropertyName, object? Value)> EnumerateValues(object source) |
| | | 27 | | { |
| | 51 | 28 | | ArgumentNullException.ThrowIfNull(source); |
| | | 29 | | |
| | 51 | 30 | | return enumerateValues(); |
| | | 31 | | |
| | | 32 | | IEnumerable<(string PropertyName, object? Value)> enumerateValues() |
| | | 33 | | { |
| | | 34 | | var getters = GetterCache.GetOrAdd(source.GetType(), CreateGetters); |
| | | 35 | | |
| | | 36 | | foreach (var entry in getters) |
| | | 37 | | yield return (entry.Name, entry.Getter(source)); |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | private static PropertyGetter[] CreateGetters(Type type) |
| | | 42 | | { |
| | 6 | 43 | | var list = (from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public) where property.CanR |
| | | 44 | | |
| | 6 | 45 | | return [.. list]; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | private static Func<object, object?> CompileGetter(Type type, PropertyInfo property) |
| | | 49 | | { |
| | 6 | 50 | | var parameter = Expression.Parameter(typeof(object)); |
| | | 51 | | |
| | 6 | 52 | | var cast = Expression.Convert(parameter, type); |
| | | 53 | | |
| | 6 | 54 | | var propertyAccess = Expression.Property(cast, property); |
| | | 55 | | |
| | 6 | 56 | | var convert = Expression.Convert(propertyAccess, typeof(object)); |
| | | 57 | | |
| | 6 | 58 | | return Expression.Lambda<Func<object, object?>>(convert, parameter).Compile(); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | private sealed class PropertyGetter(string name, Func<object, object?> getter) |
| | | 62 | | { |
| | | 63 | | public string Name { get; } = name; |
| | | 64 | | |
| | | 65 | | public Func<object, object?> Getter { get; } = getter; |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |