public static InstanceTracker Of(Object target) { var instId = target.GetInstanceId(); if (!store.TryGetValue(instId, out var tracker)) { tracker = new InstanceTracker(target); store[instId] = tracker; tracker.Freed += () => store.Remove(instId); } return(tracker); }
public static void DisposeWith(this IDisposable disposable, Object obj) { var instId = obj.GetInstanceId(); if (!objectDisposables.ContainsKey(instId)) { objectDisposables[instId] = new HashSet <IDisposable>(); InstanceTracker.Of(obj).Freed += () => { foreach (var disposable in objectDisposables[instId]) { // GD.Print($"disposed with {instId}"); disposable.Dispose(); } objectDisposables.Remove(instId); }; } objectDisposables[instId].Add(disposable); }