| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="WeakCallable.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Reflection; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Messaging; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Base class that stores a delegate (Action or Func) without creating a hard reference |
| | | 14 | | /// to the target object. The target can be garbage collected at any time. |
| | | 15 | | /// Consolidates common logic for both WeakAction and WeakFunc. |
| | | 16 | | /// </summary> |
| | | 17 | | internal abstract class WeakCallable |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets a value indicating whether the callable is static or not. |
| | | 21 | | /// </summary> |
| | | 22 | | public abstract bool IsStatic { get; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets the name of the method that this callable represents. |
| | | 26 | | /// </summary> |
| | | 27 | | public abstract string? MethodName { get; } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets a value indicating whether the callable's target is still alive. |
| | | 31 | | /// </summary> |
| | | 32 | | public abstract bool IsAlive { get; } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the target object. This object is stored as a WeakReference. |
| | | 36 | | /// </summary> |
| | | 37 | | public abstract object? Target { get; } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Marks this callable for deletion. All references are cleared. |
| | | 41 | | /// </summary> |
| | | 42 | | public abstract void MarkForDeletion(); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Stores an Action or Func without creating a hard reference to the target object. |
| | | 47 | | /// The target can be garbage collected at any time. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <typeparam name="TDelegate">The type of the delegate (Action or Func).</typeparam> |
| | | 50 | | internal sealed class WeakCallable<TDelegate> : WeakCallable |
| | | 51 | | where TDelegate : Delegate |
| | | 52 | | { |
| | | 53 | | private TDelegate? _staticDelegate; |
| | | 54 | | private WeakReference? _targetReference; |
| | | 55 | | private WeakReference? _delegateTargetReference; |
| | | 56 | | private object? _liveReference; |
| | | 57 | | private MethodInfo? _method; |
| | 18 | 58 | | private bool _isAlive = true; |
| | | 59 | | |
| | | 60 | | public WeakCallable(TDelegate @delegate, bool keepTargetAlive = false) |
| | 12 | 61 | | : this(@delegate.Target, @delegate, keepTargetAlive) |
| | | 62 | | { |
| | 12 | 63 | | } |
| | | 64 | | |
| | 18 | 65 | | public WeakCallable(object? target, TDelegate @delegate, bool keepTargetAlive = false) |
| | | 66 | | { |
| | 18 | 67 | | if (@delegate.Method.IsStatic) |
| | | 68 | | { |
| | 12 | 69 | | _staticDelegate = @delegate; |
| | 12 | 70 | | if (target != null) |
| | 0 | 71 | | _targetReference = new(target); |
| | 12 | 72 | | return; |
| | | 73 | | } |
| | | 74 | | |
| | 6 | 75 | | _method = @delegate.Method; |
| | 6 | 76 | | _delegateTargetReference = new(@delegate.Target); |
| | 6 | 77 | | _liveReference = keepTargetAlive ? @delegate.Target : null; |
| | 6 | 78 | | _targetReference = new(target); |
| | 6 | 79 | | } |
| | | 80 | | |
| | 6 | 81 | | public override bool IsStatic => _staticDelegate != null; |
| | | 82 | | |
| | | 83 | | public override string? MethodName => |
| | 3 | 84 | | _staticDelegate?.Method.Name ?? _method?.Name; |
| | | 85 | | |
| | | 86 | | public override bool IsAlive |
| | | 87 | | { |
| | | 88 | | get |
| | | 89 | | { |
| | 24 | 90 | | if (!_isAlive) |
| | 6 | 91 | | return false; |
| | | 92 | | |
| | 18 | 93 | | if (_staticDelegate == null && _targetReference == null && _liveReference == null) |
| | 0 | 94 | | return false; |
| | | 95 | | |
| | 18 | 96 | | if (_staticDelegate != null) |
| | 12 | 97 | | return _targetReference?.IsAlive != false; |
| | | 98 | | |
| | | 99 | | // Non-static delegate |
| | 6 | 100 | | return _liveReference != null || _targetReference is { IsAlive: true }; |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | public override object? Target => _targetReference?.Target; |
| | | 105 | | |
| | 6 | 106 | | private object? DelegateTarget => _liveReference ?? _delegateTargetReference?.Target; |
| | | 107 | | |
| | | 108 | | public override void MarkForDeletion() |
| | | 109 | | { |
| | 3 | 110 | | _isAlive = false; |
| | 3 | 111 | | _targetReference = null; |
| | 3 | 112 | | _delegateTargetReference = null; |
| | 3 | 113 | | _liveReference = null; |
| | 3 | 114 | | _method = null; |
| | 3 | 115 | | _staticDelegate = null; |
| | 3 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Executes the delegate with reflection if it's still alive. |
| | | 120 | | /// </summary> |
| | | 121 | | public object? Execute(params object?[] parameters) |
| | | 122 | | { |
| | 12 | 123 | | if (!IsAlive) |
| | 3 | 124 | | return null; |
| | | 125 | | |
| | 9 | 126 | | if (_staticDelegate != null) |
| | | 127 | | { |
| | | 128 | | try |
| | | 129 | | { |
| | 6 | 130 | | return _staticDelegate.DynamicInvoke(parameters); |
| | | 131 | | } |
| | 3 | 132 | | catch |
| | | 133 | | { |
| | 3 | 134 | | return null; |
| | | 135 | | } |
| | | 136 | | } |
| | | 137 | | |
| | 3 | 138 | | var target = DelegateTarget; |
| | 3 | 139 | | if (_method == null || target == null) |
| | 0 | 140 | | return null; |
| | | 141 | | |
| | | 142 | | try |
| | | 143 | | { |
| | 3 | 144 | | return _method.Invoke(target, parameters); |
| | | 145 | | } |
| | 0 | 146 | | catch |
| | | 147 | | { |
| | 0 | 148 | | return null; |
| | | 149 | | } |
| | 9 | 150 | | } |
| | | 151 | | |
| | 6 | 152 | | public TDelegate? GetDelegate() => !IsAlive || (_staticDelegate == null && (_method == null || DelegateTarget == nul |
| | | 153 | | } |
| | | 154 | | |