public T Collect <T>(T toDispose) { if (!((object)toDispose is IDisposable) && !((object)toDispose is IntPtr)) { throw new ArgumentException("Argument must be IDisposable or IntPtr"); } if ((object)toDispose is IntPtr && !Utilities.IsMemoryAligned((IntPtr)(object)toDispose, 16)) { throw new ArgumentException("Memory pointer is invalid. Memory must have been allocated with Utilties.AllocateMemory"); } Component component = (object)toDispose as Component; if (component != null && component.IsAttached || object.Equals((object)toDispose, (object)default(T))) { return(toDispose); } if (this.disposables == null) { this.disposables = new List <object>(); } if (!this.disposables.Contains((object)toDispose)) { this.disposables.Add((object)toDispose); if (component != null) { component.IsAttached = true; } } return(toDispose); }
/// <summary> /// Adds a <see cref="IDisposable"/> object or a <see cref="IntPtr"/> allocated using <see cref="Utilities.AllocateMemory"/> to the list of the objects to dispose. /// </summary> /// <param name="toDispose">To dispose.</param> /// <exception cref="ArgumentException">If toDispose argument is not IDisposable or a valid memory pointer allocated by <see cref="Utilities.AllocateMemory"/></exception> public T Collect <T>(T toDispose) { if (!(toDispose is IDisposable || toDispose is IntPtr)) { throw new ArgumentException("Argument must be IDisposable or IntPtr"); } // Check memory alignment if (toDispose is IntPtr) { var memoryPtr = (IntPtr)(object)toDispose; if (!Utilities.IsMemoryAligned(memoryPtr)) { throw new ArgumentException("Memory pointer is invalid. Memory must have been allocated with Utilties.AllocateMemory"); } } if (!Equals(toDispose, default(T))) { if (disposables == null) { disposables = new List <object>(); } if (!disposables.Contains(toDispose)) { disposables.Add(toDispose); } } return(toDispose); }
/// <summary> /// Adds a <see cref="IDisposable"/> object or a <see cref="IntPtr"/> allocated using <see cref="Utilities.AllocateMemory"/> to the list of the objects to dispose. /// </summary> /// <param name="toDispose">To dispose.</param> /// <exception cref="ArgumentException">If toDispose argument is not IDisposable or a valid memory pointer allocated by <see cref="Utilities.AllocateMemory"/></exception> public T Collect <T>(T toDispose) { if (!(toDispose is IDisposable || toDispose is IntPtr)) { throw new ArgumentException("Argument must be IDisposable or IntPtr"); } // Check memory alignment if (toDispose is IntPtr) { var memoryPtr = (IntPtr)(object)toDispose; if (!Utilities.IsMemoryAligned(memoryPtr)) { throw new ArgumentException("Memory pointer is invalid. Memory must have been allocated with Utilties.AllocateMemory"); } } var toDisposeComponent = toDispose as Component; // If this is a component and It's already attached, don't add it if (toDisposeComponent != null && toDisposeComponent.IsAttached) { return(toDispose); } if (!Equals(toDispose, default(T))) { if (disposables == null) { disposables = new List <object>(); } if (!disposables.Contains(toDispose)) { disposables.Add(toDispose); // Set attached flag for Component if (toDisposeComponent != null) { toDisposeComponent.IsAttached = true; } } } return(toDispose); }