Example #1
0
        public static T DisposeWith <T>(this T resource, IDisposeRelated withResource, Action <T> disposeAction)
        {
            Guard.ArgumentNotDefault(resource, "resource");

            var _resource = new WeakReference(resource);

            withResource.DisposeRelated += new Handler <EventArgs, EventHandler>((s, e) =>
            {
                Dispose <T>(_resource, disposeAction);
                _resource     = null;
                disposeAction = null;
            }, (h) => withResource.DisposeRelated -= h).HandleOnce();
            return(resource);
        }
Example #2
0
        public static T DisposeWith <T>(this T resource, IDisposeRelated withResource)
            where
        T : IDisposable
        {
            Guard.ArgumentNotDefault(resource, "resource");

            var _resource = new WeakReference(resource); // this outside to ensure we don't lift the handler itself

            withResource.DisposeRelated += new Handler <EventArgs, EventHandler>((s, e) =>
            {
                DisposerExtensions.Dispose <IDisposable>(_resource, _disposableDisposer);
                _resource = null;
            }, (h) => withResource.DisposeRelated -= h).HandleOnce();
            return(resource);
        }