| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Linq.Expressions; |
| | | 5 | | using System.Reflection; |
| | | 6 | | |
| | | 7 | | namespace Trailblazer.Support; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Shared implementation for transient-state synchronization and reset behavior. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// Delegates are compiled once per type on first use via expression trees, so per-call overhead |
| | | 14 | | /// is direct property assignment with no reflection at runtime. |
| | | 15 | | /// </remarks> |
| | | 16 | | internal static class TransientStateUtility |
| | | 17 | | { |
| | 1 | 18 | | private static readonly ConcurrentDictionary<Type, Action<ITransient, ITransient>> _syncDelegates = new(); |
| | 1 | 19 | | private static readonly ConcurrentDictionary<Type, Action<ITransient>> _clearDelegates = new(); |
| | | 20 | | |
| | | 21 | | internal static void Sync(ITransient instance, ITransient other) |
| | | 22 | | { |
| | 7 | 23 | | if (other == null) |
| | 1 | 24 | | throw new ArgumentNullException(nameof(other)); |
| | | 25 | | |
| | 6 | 26 | | Type sourceType = instance.GetType(); |
| | 6 | 27 | | Type targetType = other.GetType(); |
| | 6 | 28 | | if (sourceType != targetType) |
| | | 29 | | { |
| | 1 | 30 | | throw new ArgumentException( |
| | 1 | 31 | | $"Type mismatch during SyncTransientState. Expected {sourceType.FullName}, but received {targetType.Full |
| | 1 | 32 | | nameof(other)); |
| | | 33 | | } |
| | | 34 | | |
| | 5 | 35 | | _syncDelegates.GetOrAdd(sourceType, BuildSyncDelegate)(instance, other); |
| | 5 | 36 | | } |
| | | 37 | | |
| | | 38 | | internal static void Clear(ITransient instance) |
| | | 39 | | { |
| | 596 | 40 | | _clearDelegates.GetOrAdd(instance.GetType(), BuildClearDelegate)(instance); |
| | 596 | 41 | | } |
| | | 42 | | |
| | | 43 | | private static Action<ITransient, ITransient> BuildSyncDelegate(Type type) |
| | | 44 | | { |
| | 5 | 45 | | PropertyInfo[] properties = GetTransientProperties(type); |
| | 5 | 46 | | if (properties.Length == 0) |
| | 1 | 47 | | return static (_, _) => { }; |
| | | 48 | | |
| | 4 | 49 | | ParameterExpression instanceParam = Expression.Parameter(typeof(ITransient), "instance"); |
| | 4 | 50 | | ParameterExpression otherParam = Expression.Parameter(typeof(ITransient), "other"); |
| | 4 | 51 | | ParameterExpression typedInstance = Expression.Variable(type, "typedInstance"); |
| | 4 | 52 | | ParameterExpression typedOther = Expression.Variable(type, "typedOther"); |
| | | 53 | | |
| | 4 | 54 | | Expression[] body = new Expression[2 + properties.Length]; |
| | 4 | 55 | | body[0] = Expression.Assign(typedInstance, Expression.Convert(instanceParam, type)); |
| | 4 | 56 | | body[1] = Expression.Assign(typedOther, Expression.Convert(otherParam, type)); |
| | 44 | 57 | | for (int i = 0; i < properties.Length; i++) |
| | 18 | 58 | | body[i + 2] = Expression.Assign( |
| | 18 | 59 | | Expression.Property(typedInstance, properties[i]), |
| | 18 | 60 | | Expression.Property(typedOther, properties[i])); |
| | | 61 | | |
| | 4 | 62 | | BlockExpression block = Expression.Block(new[] { typedInstance, typedOther }, body); |
| | 4 | 63 | | return Expression.Lambda<Action<ITransient, ITransient>>(block, instanceParam, otherParam).Compile(); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | private static Action<ITransient> BuildClearDelegate(Type type) |
| | | 67 | | { |
| | 11 | 68 | | PropertyInfo[] properties = GetTransientProperties(type); |
| | 11 | 69 | | if (properties.Length == 0) |
| | 1 | 70 | | return static _ => { }; |
| | | 71 | | |
| | 10 | 72 | | ParameterExpression instanceParam = Expression.Parameter(typeof(ITransient), "instance"); |
| | 10 | 73 | | ParameterExpression typedVar = Expression.Variable(type, "typed"); |
| | | 74 | | |
| | 10 | 75 | | Expression[] body = new Expression[1 + properties.Length]; |
| | 10 | 76 | | body[0] = Expression.Assign(typedVar, Expression.Convert(instanceParam, type)); |
| | 108 | 77 | | for (int i = 0; i < properties.Length; i++) |
| | 44 | 78 | | body[i + 1] = Expression.Assign( |
| | 44 | 79 | | Expression.Property(typedVar, properties[i]), |
| | 44 | 80 | | GetDefaultExpression(properties[i])); |
| | | 81 | | |
| | 10 | 82 | | BlockExpression block = Expression.Block(new[] { typedVar }, body); |
| | 10 | 83 | | return Expression.Lambda<Action<ITransient>>(block, instanceParam).Compile(); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | private static Expression GetDefaultExpression(PropertyInfo property) |
| | | 87 | | { |
| | 44 | 88 | | TransientAttribute? attr = property.GetCustomAttribute<TransientAttribute>(); |
| | 44 | 89 | | if (attr?.DefaultValueSource != null && attr.DefaultValueMember != null) |
| | 4 | 90 | | return GetStaticMemberExpression(attr.DefaultValueSource, attr.DefaultValueMember); |
| | | 91 | | |
| | 40 | 92 | | return Expression.Default(property.PropertyType); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | private static Expression GetStaticMemberExpression(Type type, string memberName) |
| | | 96 | | { |
| | 4 | 97 | | FieldInfo? field = type.GetField(memberName, BindingFlags.Public | BindingFlags.Static); |
| | 4 | 98 | | if (field != null) |
| | 3 | 99 | | return Expression.Field(null, field); |
| | | 100 | | |
| | 1 | 101 | | PropertyInfo? prop = type.GetProperty(memberName, BindingFlags.Public | BindingFlags.Static); |
| | 1 | 102 | | if (prop == null) |
| | 0 | 103 | | throw new InvalidOperationException( |
| | 0 | 104 | | $"Transient default member '{type.FullName}.{memberName}' was not found."); |
| | | 105 | | |
| | 1 | 106 | | return Expression.Property(null, prop); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | private static PropertyInfo[] GetTransientProperties(Type type) |
| | | 110 | | { |
| | 16 | 111 | | return type.GetProperties() |
| | 16 | 112 | | .Where(static p => p.IsDefined(typeof(TransientAttribute), false)) |
| | 16 | 113 | | .ToArray(); |
| | | 114 | | } |
| | | 115 | | } |